【问题标题】:Not able to view tkinter form in python2无法在 python2 中查看 tkinter 表单
【发布时间】:2019-10-31 23:03:57
【问题描述】:

我在 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)

这些行可能有什么问题?

【问题讨论】:

  • 不能在同一个容器中混合 grid() 和 pack()。
  • 如何让网格占据全部空间
  • @BBRK 要使 grid 占据“全部空间”,您需要在容器上使用 rowconfigurecolumnconfigure。检查这个link

标签: tkinter pycharm python-2.x


【解决方案1】:

基于 @FrainBr33z3@acw1668 cmets,我能够使用 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()

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    相关资源
    最近更新 更多