【发布时间】:2019-04-04 02:12:21
【问题描述】:
我目前正在尝试根据我的线框创建我的窗口。然而,在使用网格时,尽管单元格中有适当的行分布,但我的按钮在它们之间会出现奇怪的空间。我很好奇,这是怎么回事?如何正确对齐这些按钮?
这是我的按钮代码:
clearButton = Button(window, text="Clear", width = 10)
clearButton.grid(row = 3, column = 3)
computeButton = Button(window, text="Compute", width=10)
computeButton.grid(row = 4, column = 3)
exitButton = Button(window, text="Exit", width = 10)
exitButton.grid(row = 5, column = 3)
下面是整个窗口的代码:
import tkinter
from tkinter import *
window = tkinter.Tk()
window.geometry('600x400')
yearLabel = Label(window, text="Year")
yearLabel.grid(row=0, column=0)
amountLabel = Label(window, text="Amount")
amountLabel.grid(row=1, column=0)
rateLabel = Label(window, text="Rate")
rateLabel.grid(row=2, column=0)
monthlyPaymentLabel = Label(window, text="Monthly Payment")
monthlyPaymentLabel.grid(row = 0, column = 3)
totalInterestLabel = Label(window, text="Total Interest Paid")
totalInterestLabel.grid(row = 1, column = 3)
yearText = StringVar()
yt = Entry(window, textvariable=yearText)
yt.grid(row=0, column=1)
amountText = StringVar()
at = Entry(window, textvariable=yearText)
at.grid(row=1, column=1)
rateText = StringVar()
rt = Entry(window, textvariable=rateText)
rt.grid(row=2, column=1)
box = Listbox(window, height = 10, width = 50)
box.grid(row = 3, column = 0, columnspan=3)
scroll = Scrollbar(window)
scroll.grid(row=2, column=2, rowspan = 6)
box.configure(yscrollcommand = scroll.set)
scroll.configure(command = box.yview)
clearButton = Button(window, text="Clear", width = 10)
clearButton.grid(row = 3, column = 3)
computeButton = Button(window, text="Compute", width=10)
computeButton.grid(row = 4, column = 3)
exitButton = Button(window, text="Exit", width = 10)
exitButton.grid(row = 5, column = 3)
window.mainloop()
【问题讨论】:
-
grid就像 Excel 中的表格 -clear与Entry大排成一行,因此它的单元格也使用了很多空格。您可能必须在网格内或包内使用网格。您可以使用一行和两列创建网格,在第一列中使用所有条目创建内部网格(或包),在第二列中使用按钮创建内部网格(或包)。 -
您需要在
box.grid(...)中指定rowspan。
标签: python user-interface tkinter