如何在tkinter中正确定位我的文本字段?

huangapple go评论84阅读模式
英文:

How do I position my text field properly in tkinter?

问题

我有一个程序,它生成了第二个窗口,我想让文本字段更靠近 'Name: ',但我不知道如何做。

def editWindow():
    
    item_choice_intvar = tk.IntVar()
    course_choice_intvar  = tk.IntVar()
    
    editMenu = tk.Toplevel()
    editMenu.geometry('400x400')
    editMenu.resizable(False,False)
    editMenu['bg'] = 'black'
    editMenu.title('Configure Menu')
    
    addItem = tk.Label(editMenu,text = "Add Item:",font = ("Garamond",20),bg = 'black')
    addItem.grid(column = 0,row = 0, sticky = 'WN', pady = 10, padx = 20)
    
    itemChoice = tk.Radiobutton(editMenu,text = 'Menu Item',variable=item_choice_intvar,value = 1,bg = 'black') 
    dealChoice = tk.Radiobutton(editMenu,text = 'Special Deal',variable=item_choice_intvar,value = 2,bg = 'black')
    itemChoice.grid(column = 0,row=1)
    dealChoice.grid(column = 1,row=1)
    
    courseText = tk.Label(editMenu,text = "Select Course:",font = ("Garamond",20),bg = 'black')
    courseText.grid(column = 0,row = 2, sticky = 'N', pady = 10, padx = 20)
    
    entreeChoice = tk.Radiobutton(editMenu,text = 'Entree',variable=course_choice_intvar,value = 1,bg = 'black') 
    mainChoice = tk.Radiobutton(editMenu,text = 'Main',variable=course_choice_intvar,value = 2,bg = 'black')
    dessertChoice = tk.Radiobutton(editMenu,text = 'Dessert',variable=course_choice_intvar,value = 3,bg = 'black') 
    
    entreeChoice.grid(column = 0, row = 3)
    mainChoice.grid(column = 1, row = 3,sticky = 'W')
    dessertChoice.grid(column = 2, row = 3,sticky = 'W')
    
    itemNameText = tk.Label(editMenu,text = 'Name: ',font = ("Garamond",15),bg = 'black')
    nameTextField = tk.Text(editMenu,height=1,width=15)
    itemNameText.grid(column = 0, row = 4,sticky='W',padx = 25,pady = 30)
    nameTextField.grid(column = 1, row = 4,sticky = 'W')

我尝试过使用 stickypad,但它们似乎没有起作用。

英文:

I have a program which spawns a second window and I want the text field to be closer to 'Name: ' but I don't know how to go about it.

def editWindow():
item_choice_intvar = tk.IntVar()
course_choice_intvar  = tk.IntVar()
editMenu = tk.Toplevel()
editMenu.geometry('400x400')
editMenu.resizable(False,False)
editMenu['bg'] = 'black'
editMenu.title('Configure Menu')
addItem = tk.Label(editMenu,text = "Add Item:",font = ("Garamond",20),bg = 'black')
addItem.grid(column = 0,row = 0, sticky = 'WN', pady = 10, padx = 20)
itemChoice = tk.Radiobutton(editMenu,text = 'Menu Item',variable=item_choice_intvar,value = 1,bg = 'black') 
dealChoice = tk.Radiobutton(editMenu,text = 'Special Deal',variable=item_choice_intvar,value = 2,bg = 'black')
itemChoice.grid(column = 0,row=1)
dealChoice.grid(column = 1,row=1)
courseText = tk.Label(editMenu,text = "Select Course:",font = ("Garamond",20),bg = 'black')
courseText.grid(column = 0,row = 2, sticky = 'N', pady = 10, padx = 20)
entreeChoice = tk.Radiobutton(editMenu,text = 'Entree',variable=course_choice_intvar,value = 1,bg = 'black') 
mainChoice = tk.Radiobutton(editMenu,text = 'Main',variable=course_choice_intvar,value = 2,bg = 'black')
dessertChoice = tk.Radiobutton(editMenu,text = 'Dessert',variable=course_choice_intvar,value = 3,bg = 'black') 
entreeChoice.grid(column = 0, row = 3)
mainChoice.grid(column = 1, row = 3,sticky = 'W')
dessertChoice.grid(column = 2, row = 3,sticky = 'W')
itemNameText = tk.Label(editMenu,text = 'Name: ',font = ("Garamond",15),bg = 'black')
nameTextField = tk.Text(editMenu,height=1,width=15)
itemNameText.grid(column = 0, row = 4,sticky='W',padx = 25,pady = 30)
nameTextField.grid(column = 1, row = 4,sticky = 'W')

I've tried using sticky and pad but they don't seem to be helping.

如何在tkinter中正确定位我的文本字段?

答案1

得分: 2

只需将标签和文本框放在另一个框架内:

def editWindow():
    ...
    # 用于容纳标签和文本框的框架
    frame = tk.Frame(editMenu, bg='black')
    frame.grid(row=4, column=0, columnspan=3, sticky='W')

    itemNameText = tk.Label(frame, text='Name:', font=("Garamond", 15), bg='black', fg='white')
    nameTextField = tk.Text(frame, height=1, width=15) # 为什么不使用 Entry 组件呢?
    itemNameText.pack(side='left', padx=25, pady=30)
    nameTextField.pack(side='left')

结果:

如何在tkinter中正确定位我的文本字段?

英文:

Simply put the label and text box inside another frame:

def editWindow():
    ...
    # frame to hold the label and text box
    frame = tk.Frame(editMenu, bg='black')
    frame.grid(row=4, column=0, columnspan=3, sticky='W')

    itemNameText = tk.Label(frame, text='Name:', font=("Garamond",15), bg= 'black', fg='white')
    nameTextField = tk.Text(frame, height=1, width=15) # why not use Entry widget instead?
    itemNameText.pack(side='left', padx=25, pady=30)
    nameTextField.pack(side='left')

Result:

如何在tkinter中正确定位我的文本字段?

huangapple
  • 本文由 发表于 2023年7月6日 15:10:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定