GUI未显示在桌面上

时间:2019-05-01 00:16:45

标签: python tkinter

当我运行程序时,GUI没有出现在桌面上

from tkinter import *
root = Tk()

label1 = Label(root, text="Bench")
label2 = Label(root, text="Deadlift")
label3 = Label(root, text="Squat")
label1.grid(row=0, sticky=E)
label2.grid(row=1, sticky=E)
label3.grid(row=2, sticky=E)
e1 = Entry(root)
e2 = Entry(root)
e3 = Entry(root)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
l2 = Label(root)


def callback():
    total = sum(int(e.get()) for e in (e1, e2, e3))
    l2.config(text="answer = %s" % total)


b1 = Button(root, text="enter", command=callback)
b1.pack()
l2.pack()

root.mainloop()

我希望GUI出现。任何答案都非常感谢,我是一个初学者,正在尝试解决这个问题。

1 个答案:

答案 0 :(得分:1)

当您使用.pack和.grid时发生冲突。您只需将.pack更改为.grid并修复def Label,.config并不是标签的有效参数,因此将其放入函数中即可解决问题。

from tkinter import *
root = Tk()

label1 = Label(root, text="Bench")
label2 = Label(root, text="Deadlift")
label3 = Label(root, text="Squat")
label1.grid(row=0, sticky=E)
label2.grid(row=1, sticky=E)
label3.grid(row=2, sticky=E)


e1 = Entry(root)
e2 = Entry(root)
e3 = Entry(root)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)


def callback():
    total = sum(int(e.get()) for e in (e1, e2, e3))
    Label(root, text="answer = %s" % total).grid(row=3, column=1)


b1 = Button(root, text="enter", command=callback).grid(row=3)


root.mainloop()

我希望这会有所帮助:)