2016-08-04 94 views
1

目的: 我想创建一个具有“选项卡”(显示的信息可以根据选定的选项卡改变的)屏幕的一部分的图形用户界面,和另一部分不断显示相同的东西。蟒蛇Tkinter制表符和画布

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

问题:

  1. 在当前配置的翼片位于屏幕的未在左侧的中部。

  2. 如果我将canvas.grid()更改为canvas.pack()它实际上并不打开任何窗口?

  3. 画布上的矩形没有出现!

谢谢。

回答

0
  1. 要做到这一点,网格您的笔记本电脑的时候,传递参数column,选择0,所以,这将是位于最左边,就像这样:

    nb.grid(column=0)

  2. 这是因为您必须为您的tkinter应用程序选择.grid().pack()之间:两者不兼容。由于您以前使用过.grid(),因此该窗口将不会打开并弹出一个TclError

  3. 您的画布实际上隐藏在笔记本下。为了解决这个问题,使用grid为0时,设置row参数,因此,它是在顶部,这样的:

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

我希望这帮助!

+0

点#1是误导。例如,你说要使用第0列,但使用第1列。第二,仅仅因为它位于最左边的列中,不能保证它与窗口的左边缘相对。 –

+0

@BryanOakley对不起,由于指出了错字。对于第二点,到目前为止,我的意思是笔记本电脑和边缘之间没有任何东西,但不一定会卡住它。 – TrakJohnson

+0

谢谢@TrakJohnson!完美的答案,正是我想要的。后续问题:假设我想要一个矩形画在选项卡上(我可以拖动到画布上)。我已经有了拖动代码在画布上工作,但我无法在“选项卡”上绘制任何矩形 – smarttylerthecreator