每行都紧贴下一个-Tkinter

时间:2019-04-21 12:40:23

标签: python tkinter

我正在创建GUI,但遇到了问题。尽管我在row=17row=40处有按钮,但它们彼此粘在一起。例如: 灰色按钮位于第17行,Proceed按钮位于第40行。尽管如此,它看起来像这样:

enter image description here

那是为什么?为什么在row = 17和row = 40的情况下,每一行都粘在另一行上。为什么它们之间没有空间?

代码如下所示(此行= 17,行= 40在代码末尾):

gui = Tk()
gui.geometry('600x500')
gui.title("Vending Machine")

# gui.configure(background="white")
expression = ""
equation = StringVar()
expression_field = Entry(gui, textvariable=equation)
expression_field.grid(row=8, columnspan=4, ipadx=70)
equation.set('enter number of item')

labelOne = ttk.Label(gui, text='')
labelOne.grid(column=1, row=12)

labelTwo = ttk.Label(gui, text='')
labelTwo.grid(column=1, row=18)

labelThree = ttk.Label(gui, text='')
labelThree.grid(column=1, row=0)
labelThree.configure(text='Wpisz numer produktu!')


def dropCoin(value):
    labelTwo.configure(text='Wrzucono: {0:0.2f}'.format(container.howManyCoinsWereDropped(value)))


def getPriceOfGivenID():
    givenID = int(equation.get())
    labelOne.configure(text='Musisz wrzucić: ' + str(container.find_price_of_given_id(givenID)))


def press(num):
    global expression
    expression = expression + str(num)
    equation.set(expression)
    return expression


def clear():
    global expression
    expression = ""
    equation.set("")
    labelOne.configure(text='')


##### BUTTONS FOR GIVING ID
button1 = Button(gui, text=' 1 ', fg='black', bg='light blue',
                 command=lambda: press(1), height=1, width=7)
button1.grid(row=2, column=0)

button2 = Button(gui, text=' 2 ', fg='black', bg='light blue',
                 command=lambda: press(2), height=1, width=7)
button2.grid(row=2, column=1)

button3 = Button(gui, text=' 3 ', fg='black', bg='light blue',
                 command=lambda: press(3), height=1, width=7)
button3.grid(row=2, column=2)

button4 = Button(gui, text=' 4 ', fg='black', bg='light blue',
                 command=lambda: press(4), height=1, width=7)
button4.grid(row=3, column=0)

button5 = Button(gui, text=' 5 ', fg='black', bg='light blue',
                 command=lambda: press(5), height=1, width=7)
button5.grid(row=3, column=1)

button6 = Button(gui, text=' 6 ', fg='black', bg='light blue',
                 command=lambda: press(6), height=1, width=7)
button6.grid(row=3, column=2)

button7 = Button(gui, text=' 7 ', fg='black', bg='light blue',
                 command=lambda: press(7), height=1, width=7)
button7.grid(row=4, column=0)

button8 = Button(gui, text=' 8 ', fg='black', bg='light blue',
                 command=lambda: press(8), height=1, width=7)
button8.grid(row=4, column=1)

button9 = Button(gui, text=' 9 ', fg='black', bg='light blue',
                 command=lambda: press(9), height=1, width=7)
button9.grid(row=4, column=2)

button0 = Button(gui, text=' 0 ', fg='black', bg='light blue',
                 command=lambda: press(0), height=1, width=7)
button0.grid(row=5, column=1)

button10 = Button(gui, text=' Proceed ', fg='black', bg='red',
                  command=lambda: getPriceOfGivenID(), height=1, width=7)
button10.grid(row=10, column=1)

button11 = Button(gui, text=' Clear ', fg='black', bg='orange',
                  command=lambda: clear(), height=1, width=7)
button11.grid(row=10, column=2)

##BUTONS FOR DROPING COINS

Coin1 = Button(gui, text=' 0.01 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("0.01"), height=1, width=7)
Coin1.grid(row=15, column=0)

Coin2 = Button(gui, text=' 0.02 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("0.02"), height=1, width=7)
Coin2.grid(row=15, column=1)

Coin3 = Button(gui, text=' 0.05 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("0.05"), height=1, width=7)
Coin3.grid(row=15, column=2)

Coin4 = Button(gui, text=' 0.1 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("0.1"), height=1, width=7)
Coin4.grid(row=16, column=0)

Coin5 = Button(gui, text=' 0.2 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("0.2"), height=1, width=7)
Coin5.grid(row=16, column=1)

Coin6 = Button(gui, text=' 0.5 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("0.5"), height=1, width=7)
Coin6.grid(row=16, column=2)

Coin7 = Button(gui, text=' 1 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("1"), height=1, width=7)
Coin7.grid(row=17, column=0)

Coin8 = Button(gui, text=' 2 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("2"), height=1, width=7)
Coin8.grid(row=17, column=1)

Coin9 = Button(gui, text=' 5 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("5"), height=1, width=7)
Coin9.grid(row=17, column=2)

button11 = Button(gui, text=' Proceed ', fg='black', bg='red',
                  command=lambda: press(0), height=1, width=7)
button11.grid(row=40, column=1)

mainframe = ttk.Frame(gui)

1 个答案:

答案 0 :(得分:1)

空行没有高度,空列没有宽度。

如果您将空Widgetheight=一起放置(例如,空FrameLabel没有文本),则行将具有其高度,并且您将其视为空白

使用gui.rowconfigure(row_number, minsize=...)可以为单行设置最小高度。如果您在for循环中执行此操作,则可以为所有行设置它。

enter image description here

使用gui.columnconfigure(colum_number, minsize=...)可以为单列设置最小宽度。如果您在for循环中执行此操作,则可以为所有列进行设置。

代码:

from tkinter import *
from tkinter import ttk

gui = Tk()
gui.geometry('600x500')
gui.title("Vending Machine")

for x in range(150):
    gui.rowconfigure(x, minsize=10)

# gui.configure(background="white")
expression = ""
equation = StringVar()
expression_field = Entry(gui, textvariable=equation)
expression_field.grid(row=8, columnspan=4, ipadx=70)
equation.set('enter number of item')

labelOne = ttk.Label(gui, text='')
labelOne.grid(column=1, row=12)

labelTwo = ttk.Label(gui, text='')
labelTwo.grid(column=1, row=18)

labelThree = ttk.Label(gui, text='')
labelThree.grid(column=1, row=0)
labelThree.configure(text='Wpisz numer produktu!')


def dropCoin(value):
    labelTwo.configure(text='Wrzucono: {0:0.2f}'.format(container.howManyCoinsWereDropped(value)))


def getPriceOfGivenID():
    givenID = int(equation.get())
    labelOne.configure(text='Musisz wrzucić: ' + str(container.find_price_of_given_id(givenID)))


def press(num):
    global expression
    expression = expression + str(num)
    equation.set(expression)
    return expression


def clear():
    global expression
    expression = ""
    equation.set("")
    labelOne.configure(text='')


##### BUTTONS FOR GIVING ID
button1 = Button(gui, text=' 1 ', fg='black', bg='light blue',
                 command=lambda: press(1), height=1, width=7)
button1.grid(row=2, column=0)

button2 = Button(gui, text=' 2 ', fg='black', bg='light blue',
                 command=lambda: press(2), height=1, width=7)
button2.grid(row=2, column=1)

button3 = Button(gui, text=' 3 ', fg='black', bg='light blue',
                 command=lambda: press(3), height=1, width=7)
button3.grid(row=2, column=2)

button4 = Button(gui, text=' 4 ', fg='black', bg='light blue',
                 command=lambda: press(4), height=1, width=7)
button4.grid(row=3, column=0)

button5 = Button(gui, text=' 5 ', fg='black', bg='light blue',
                 command=lambda: press(5), height=1, width=7)
button5.grid(row=3, column=1)

button6 = Button(gui, text=' 6 ', fg='black', bg='light blue',
                 command=lambda: press(6), height=1, width=7)
button6.grid(row=3, column=2)

button7 = Button(gui, text=' 7 ', fg='black', bg='light blue',
                 command=lambda: press(7), height=1, width=7)
button7.grid(row=4, column=0)

button8 = Button(gui, text=' 8 ', fg='black', bg='light blue',
                 command=lambda: press(8), height=1, width=7)
button8.grid(row=4, column=1)

button9 = Button(gui, text=' 9 ', fg='black', bg='light blue',
                 command=lambda: press(9), height=1, width=7)
button9.grid(row=4, column=2)

button0 = Button(gui, text=' 0 ', fg='black', bg='light blue',
                 command=lambda: press(0), height=1, width=7)
button0.grid(row=5, column=1)

button10 = Button(gui, text=' Proceed ', fg='black', bg='red',
                  command=lambda: getPriceOfGivenID(), height=1, width=7)
button10.grid(row=10, column=1)

button11 = Button(gui, text=' Clear ', fg='black', bg='orange',
                  command=lambda: clear(), height=1, width=7)
button11.grid(row=10, column=2)

##BUTONS FOR DROPING COINS

Coin1 = Button(gui, text=' 0.01 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("0.01"), height=1, width=7)
Coin1.grid(row=15, column=0)

Coin2 = Button(gui, text=' 0.02 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("0.02"), height=1, width=7)
Coin2.grid(row=15, column=1)

Coin3 = Button(gui, text=' 0.05 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("0.05"), height=1, width=7)
Coin3.grid(row=15, column=2)

Coin4 = Button(gui, text=' 0.1 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("0.1"), height=1, width=7)
Coin4.grid(row=16, column=0)

Coin5 = Button(gui, text=' 0.2 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("0.2"), height=1, width=7)
Coin5.grid(row=16, column=1)

Coin6 = Button(gui, text=' 0.5 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("0.5"), height=1, width=7)
Coin6.grid(row=16, column=2)

Coin7 = Button(gui, text=' 1 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("1"), height=1, width=7)
Coin7.grid(row=17, column=0)

Coin8 = Button(gui, text=' 2 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("2"), height=1, width=7)
Coin8.grid(row=17, column=1)

Coin9 = Button(gui, text=' 5 ', fg='black', bg='seashell4',
               command=lambda: dropCoin("5"), height=1, width=7)
Coin9.grid(row=17, column=2)

button11 = Button(gui, text=' Proceed ', fg='black', bg='red',
                  command=lambda: press(0), height=1, width=7)
button11.grid(row=40, column=1)

mainframe = ttk.Frame(gui)

gui.mainloop()