Tkinter按钮搞砸了GUI?

时间:2018-07-10 03:54:38

标签: python-2.7 tkinter

我正在使用tkinter(Python 2.7)进行简单的登录。

我一直在尝试调试此问题,并且找出了导致此问题的原因。

基本上,问题是当我从GUI中删除Tkinter按钮时,它可以很好地启动。但是,当我重新添加它时,我什至看不到GUI或任何东西。这是我的代码:

    print ("Loading...")
    from Tkinter import *
    root = Tk()
    root.title("Login Box")
    root.geometry("350x100")
    L1 = Label(root, text = "Username :").grid(row=0)
    L2 = Label(root, text = "Password : ").grid(row=1)
    Username = Entry(root).grid(row=0, column = 1)
    Password = Entry(root).grid(row = 1, column = 1)
    LoginButton = Button(root, text = "Login & Show Data!", command=Confirm) #Run confirm code.
    LoginButton.pack()
    root.mainloop()

因此,如果我跑步,则看不到它。但是,删除“ LogingButton”便可以使用。

感谢您的帮助, 谢谢。

1 个答案:

答案 0 :(得分:0)

首先,不要同时使用pack()和grid()。当我运行您的程序时,出现以下错误:

Traceback (most recent call last):
  File "ButtonAddition.py", line 10, in <module>
    LoginButton.pack()
  File "/home/eshita/anaconda3/lib/python3.6/tkinter/__init__.py", line 2140, in pack_configure
    + self._options(cnf, kw))
_tkinter.TclError: cannot use geometry manager pack inside . which already has slaves managed by grid

所以

from tkinter import *
root = Tk()
root.title("Login Box")
root.geometry("350x100")
L1 = Label(root, text = "Username :").grid(row=0)
L2 = Label(root, text = "Password : ").grid(row=1)
Username = Entry(root).grid(row=0, column = 1)
Password = Entry(root).grid(row = 1, column = 1)
LoginButton = Button(root, text = "Login & Show Data!", command=Confirm) #Run confirm code.
LoginButton.grid()          # Change the pack() to grid()
root.mainloop()

以上更改将消除错误。希望这会有所帮助。