目标: 我正在尝试创建一个GUI,屏幕的一部分有"标签" (显示的信息可以根据选定的标签进行更改),另一部分会不断显示相同的内容。
import ttk
import Tkinter
def demo():
#root = tk.Tk()
schedGraphics = Tkinter
root = schedGraphics.Tk()
root.title("Testing Bot")
universal_height = 606
canvas = schedGraphics.Canvas(root,width = 900, height = universal_height)
nb = ttk.Notebook(root)
# adding Frames as pages for the ttk.Notebook
# first page, which would get widgets gridded into it
page1 = ttk.Frame(nb,width = 300,height = universal_height)
# second page
page2 = ttk.Frame(nb,width = 300,height = universal_height)
nb.add(page1, text='One')
nb.add(page2, text='Two')
#
nb.grid()
day_label = schedGraphics.Label(page1, text="Day1:")
day_label.pack()
day_label.place(x=0, y=30)
day_label = schedGraphics.Label(page2, text="Day2:")
day_label.pack()
day_label.place(x=0, y=30)
canvas.create_rectangle(50,500,300,600,fill = "red")
canvas.grid()
root.mainloop()
if __name__ == "__main__":
demo()
问题:
在当前配置中,标签位于屏幕的MIDDLE中,而不在左侧。
如果我将canvas.grid()更改为canvas.pack(),它实际上并没有打开任何窗口吗?
画布上的矩形不会出现!
谢谢。
答案 0 :(得分:2)
为此,在对笔记本进行网格化时,传递参数column
并选择0,使其位于最左侧,如下所示:
nb.grid(column=0)
那是因为您必须在.grid()
和.pack()
之间为您的tkinter应用选择:两者不兼容。如果您之前使用.grid()
,则窗口将无法打开,并会弹出TclError
。
您的画布实际上隐藏在笔记本下面。要解决此问题,请在将row
设置为0时设置grid
参数,使其位于顶部,如下所示:
canvas.grid(column=1, row=0)
最终代码:
import Tkinter
import ttk
def demo():
#root = tk.Tk()
schedGraphics = Tkinter
root = schedGraphics.Tk()
root.title("Testing Bot")
universal_height = 606
nb = ttk.Notebook(root)
# adding Frames as pages for the ttk.Notebook
# first page, which would get widgets gridded into it
page1 = ttk.Frame(nb, width= 300,height = universal_height)
# second page
page2 = ttk.Frame(nb,width = 300,height = universal_height)
nb.add(page1, text='One')
nb.add(page2, text='Two')
nb.grid(column=0)
day_label = schedGraphics.Label(page1, text="Day1:")
day_label.pack()
day_label.place(x=0, y=30)
day_label = schedGraphics.Label(page2, text="Day2:")
day_label.pack()
day_label.place(x=0, y=30)
canvas = schedGraphics.Canvas(root, width=900, height=universal_height)
canvas.create_rectangle(50, 500, 300, 600, fill="red")
canvas.grid(column=1, row=0)
root.mainloop()
if __name__ == "__main__":
demo()
我希望这有帮助!