使用TkInter时,标签,按钮和条目之间存在随机空间

时间:2018-07-28 01:00:52

标签: python python-3.x tkinter

Python 3

我正在使用Tkinter,并且得到了意外的结果。我想做的是 不同的标签,按钮和输入框,以更好地练习我鲜为人知的Tkinter技能。

这是我的代码:

from tkinter import *

def display_music(event):
    music = favoriteMusician.get()

    outputEntry.delete(0, 'end')

    outputEntry.insert(0, 'Your favorite musician is %s' % music)

def divide(event):
    answer = str(int(num1Entry.get())/int(num2Entry.get()))
    divideEntry.delete(0, 'end')
    divideEntry.insert(0, answer)


root = Tk()

Label(root, text='Who is your favorite musician?').grid(row=0, column=0, sticky=W)

favoriteMusician = Entry(root)
favoriteMusician.grid(row=0, column=2, sticky=W)

b1 = Button(root, text='Output:')
b1.bind('<Button-1>', display_music)
b1.grid(row=1, column=0, sticky=W)

outputEntry = Entry(root)
outputEntry.grid(row=1, column=1, columnspan=2, sticky=W)

num1Entry = Entry(root)
num1Entry.grid(row=2, column=0, sticky=W)

Label(root, text='/').grid(row=2, column=1, sticky=W)

num2Entry = Entry(root)
num2Entry.grid(row=2, column=2, sticky=W)

b2 = Button(root, text='=')
b2.bind('<Button-1>', divide)
b2.grid(row=2, column=3, sticky=W)


divideEntry = Entry(root)
divideEntry.grid(row=2, column=4, sticky=W)


root.mainloop()

但是当我运行代码时。存在布局问题,我希望所有内容都对齐,但不是所有内容都对齐,还有很多我不知道如何消除的空白。当前的外观如下:

Screenshot of currently displayed GUI

我了解这与Tkinter基于现有单元格的大小决定(grid())的单元格大小有关。我已经做过研究,但是找不到答案。

除了答案之外,我还可以提供有关如何编写更好的代码的反馈,也可以提供一些免费学习python的资源。

2 个答案:

答案 0 :(得分:0)

您只需要确保columnspan在网格的不同单元格中匹配即可。此代码应产生所需的输出:     从tkinter导入*

def display_music(event):
    music = favoriteMusician.get()
    outputEntry.delete(0, 'end')
    outputEntry.insert(0, 'Your favorite musician is %s' % music)

def divide(event):
    answer = str(int(num1Entry.get())/int(num2Entry.get()))
    divideEntry.delete(0, 'end')
    divideEntry.insert(0, answer)


root = Tk()
Label(root, text='Who is your favorite musician?').grid(row=0, column=0, columnspan=2, sticky=W)

favoriteMusician = Entry(root, width=30)
favoriteMusician.grid(row=0, column=2, columnspan=5, sticky=W)

b1 = Button(root, text='Output:')
b1.bind('<Button-1>', display_music)
b1.grid(row=1, column=0, columnspan=2, sticky=W)

outputEntry = Entry(root, width=30)
outputEntry.grid(row=1, column=2, columnspan=2, sticky=W)




num1Entry = Entry(root)
num1Entry.grid(row=2, column=0, columnspan=1, sticky=W)

Label(root, text='/').grid(row=2, column=1, columnspan=1, sticky=W)

num2Entry = Entry(root)
num2Entry.grid(row=2, column=2,columnspan=1, sticky=W)

b2 = Button(root, text='=')
b2.bind('<Button-1>', divide)
b2.grid(row=2, column=3, columnspan=1, sticky=W)


divideEntry = Entry(root)
divideEntry.grid(row=2, column=4, columnspan=1, sticky=W)


root.mainloop()

enter image description here

答案 1 :(得分:0)

使用grid()布局管理器时,有多种方法可以使小部件在Tkinter中对齐。对于同一行上的两个窗口小部件,可以给第一个窗口小部件提供sticky=E选项,而给后一个窗口小部件提供sticky=W。这将把两者推到一起。我已经对前两行中的小部件执行了此操作。

另一种方法是使整个行成为嵌套的Frame,其中包含所有小部件。嵌套框架内的布局与包含Frame的位置无关。我是针对最后的四个小部件进行的。

尽管它与对齐或布局无关,但我也将display_music()divide()函数更改为Button回调(使用它们接受的command=xxx参数)而是通过bind()将它们附加到特定的小部件来使它们成为事件的事件处理程序(这意味着它们将不再收到event自变量,无论如何您都不会使用它)。

from tkinter import *

# Button callbacks (which are NOT event handlers)
def display_music():
    music = favoriteMusician.get()
    outputEntry.delete(0, 'end')
    outputEntry.insert(0, 'Your favorite musician is %s' % music)

def divide():
    answer = str(int(num1Entry.get())/int(num2Entry.get()))
    divideEntry.delete(0, 'end')
    divideEntry.insert(0, answer)


root = Tk()

# Row 0
label1 = Label(root, text='Who is your favorite musician?')
label1.grid(row=0, column=0, sticky=E)

favoriteMusician = Entry(root)
favoriteMusician.grid(row=0, column=1, sticky=W)

# Row 1
b1 = Button(root, text='Output:', command=display_music)
b1.grid(row=1, column=0, sticky=E)
#b1.bind('<Button-1>', display_music)

outputEntry = Entry(root, width=30)
#outputEntry.grid(row=1, column=1, columnspan=2, sticky=W)
outputEntry.grid(row=1, column=1, sticky=W)

# Row 2
mathFrame = Frame(root)
mathFrame.grid(row=2, column=0, columnspan=4)

num1Entry = Entry(mathFrame)
num1Entry.grid(row=2, column=0, sticky=W)

label2 = Label(mathFrame, text='/')
label2.grid(row=2, column=1, sticky=W)

num2Entry = Entry(mathFrame)
num2Entry.grid(row=2, column=2, sticky=W)

b2 = Button(mathFrame, text='=', command=divide)
b2.grid(row=2, column=3, sticky=W)
#b2.bind('<Button-1>', divide)

divideEntry = Entry(mathFrame)
divideEntry.grid(row=2, column=4, sticky=W)


root.mainloop()

这是运行时的样子:

screenshot of GUI using revised code

以下是有关Tkinter相关文章的几个链接:

The Tkinter Grid Geometry Manager,作者:Fredrik Lundh(Tkinter的作者)
Tkinter 8.5 reference: a GUI for Python,约翰·希普曼(新墨西哥科技计算机中心)