无法在python2中查看tkinter表单

时间:2019-06-18 10:13:56

标签: tkinter pycharm python-2.x

我正在使用 Python2 中的 Tkinter 创建表单的UI,其中IDE为 PyCharm

以下是参考代码:-

from Tkinter import *;

root = Tk()

root.geometry("400x100")

#adding the header label of the tool.
myheaderTitle = Label(root,text="myForm",bg="lime green",font="Calibri 16 bold",width="400")
myheaderTitle.pack()

button_1 = Button(root,text="Select File")
button_1.grid(row=0,column=0)
root.mainloop()

我已经成功运行了上面的代码,但是每当我在下面两行代码中添加以下内容时,表单就不可见。

button_1 = Button(root,text="Select File")
button_1.grid(row=0,column=0)

这些行可能是什么问题?

1 个答案:

答案 0 :(得分:0)

基于@FrainBr33z3@acw1668的注释,我能够在 Python2 中使用网格系统创建UI。

我使用以下参考链接来了解网格系统。

以下是参考代码:-

from Tkinter import *;

root = Tk()

root.geometry("400x100")

#adding the header label of the tool.
myheaderTitle = Label(root,text="myForm",bg="lime green",font="Calibri 16 bold",width="400")
myheaderTitle.grid(row=0,column=0,sticky=(N, S, W, E),padx=5, pady=5,columnspan=2)

button_1 = Button(root,text="Select File")
button_1.grid(row=1,column=0,sticky=(N, S, W, E),padx=5, pady=5)

button_2 = Button(root,text="Select Another File")
button_2.grid(row=1,column=0,sticky=(N, S, W, E),padx=5, pady=5)

root.grid_columnconfigure(0,weight=1)
root.grid_rowconfigure(0,weight=1)
root.grid_columnconfigure(1,weight=1)
root.mainloop()

希望这对某人有帮助。