简体   繁体   English

Python Tkinter选项卡和画布

[英]Python Tkinter Tabs and Canvas

Objective: I am trying to create a GUI with a portion of the screen having "tabs" (information displayed can be changed based on selected tab), and another portion constantly displaying the same thing. 目标:我正在尝试创建一个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()

Problems: 问题:

  1. In the current configuration the tabs are located in the MIDDLE of the screen not on the left side. 在当前配置中,标签位于屏幕的MIDDLE中,而不是在左侧。

  2. If I change canvas.grid() to canvas.pack() it doesn't actually open any window? 如果我将canvas.grid()更改为canvas.pack(),它实际上不会打开任何窗口?

  3. The rectangle on canvas does not appear! 画布上的矩形不会出现!

Thank you. 谢谢。

  1. To do this, when gridding your notebook, pass the argument column and choose 0, so that it will be located at the far left, like this: 为此,在对笔记本进行网格化时,传递参数column并选择0,使其位于最左侧,如下所示:

    nb.grid(column=0)

  2. That's because you have to chose, for your tkinter app, between .grid() and .pack() : the two are not compatible. 那是因为你必须为你的.grid() app选择.grid().pack() :这两者是不兼容的。 As you used .grid() before, the window won't open and a TclError pops up. 正如您之前使用.grid()一样,窗口将无法打开并弹出TclError

  3. Your canvas is in fact hidden under the notebook. 您的画布实际上隐藏在笔记本下面。 To fix that, set the row argument when using grid to 0, so that it is at the top, like this: 要解决这个问题,请在将grid为0时设置row参数,使其位于顶部,如下所示:

    canvas.grid(column=1, row=0)

Final code: 最终代码:

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()

I hope this helps ! 我希望这有帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM