2015-04-23 23 views
0

我爲我正在設計的設計創建了一個GUI,並且畫布在新窗口中彈出。我試圖讓它出現在按鈕下方。我不能讓畫布工作,我想如何去

__author__ = 'Isaac Alderton' 
__authorEmail__ = '[email protected]' 
from Tkinter import * 

class Application(Frame): 
""" My smart board application""" 
def __init__(self, master): 
    Frame.__init__(self, master) 
    self.grid() 
    self.create_widgets() 

def create_widgets(self): 

#Button1 
#buttons ment to span across the width of the page 
#needs linking to google calander 
    self.button1 = Button(self, text = "  Calandar  ") 
    self.button1.grid(row=1,column = 1) 

#button2 
    self.button2 = Button(self, text = "  weather  ") 
    self.button2.grid(row=1,column = 2) 

#button3 
#defult home menu in the future user sets a new home 
    self.button3 = Button(self, text = "  Whiteboard  ") 
    self.button3.grid(row=1,column = 3) 

#button4 
    self.button4 = Button(self, text = "  Internet  ") 
    self.button4.grid(row=1,column = 4) 

#button5 
    self.button5 = Button(self, text = "  Settings  ") 
    self.button5.grid(row=1,column = 5) 
#def settings(): 
    #pop up window where the settings options have wifi screen brightness  sleep options 
    #and also account settings 
    #One day also ui changes so color themes etc 

#Canvas 
#canvas ment to be part of whiteboard screen not a "pop-up" 
#canvas is ment to fill the whole page 


def xy(event): 
    global lastx, lasty 
    lastx, lasty = event.x, event.y 

def addLine(event): 
    global lastx, lasty 
    canvas.create_line((lastx, lasty, event.x, event.y)) 
    lastx, lasty = event.x, event.y 

root = Tk() 
root.columnconfigure(0, weight=1) 
root.rowconfigure(0, weight=1) 

canvas = Canvas(root) 
canvas.grid(column=5, row=4,) 
canvas.bind("<Button-1>", xy) 
canvas.bind("<B1-Motion>", addLine) 





#endof canvas 
root = Tk() 
root.title("Smartboard") 
#Geometry is ment to fit device???? 

root.geometry("530x500") 
app = Application(root) 

root.mainloop() 


# Copyright {2015} {Isaac Alderton} 

回答

0

你的問題是,你的代碼的最後一節是:

root = Tk() 
#... 
canvas = Canvas(root) 
#... 
#endof canvas 
root = Tk() 
#... 
app = Application(root) 

您正在創建一個窗口,增加你的畫布,然後創建一個新的窗口,並添加幀到新窗口。如果你註釋掉第二個root = Tk(),它可以正常工作。調整佈局:

__author__ = 'Isaac Alderton' 
__authorEmail__ = '[email protected]' 
from Tkinter import * 

class Application(Frame): 
    """ My smart board application""" 
    def __init__(self, master): 
     Frame.__init__(self, master) 
     self.grid(column=0, row=0, sticky="ew") 
     self.create_widgets() 
     for i in range(5): 
      self.columnconfigure(i, weight=1) 
     self.rowconfigure(0, weight=1) 

    def create_widgets(self): 

    #Button1 
    #buttons ment to span across the width of the page 
    #needs linking to google calander 
     self.button1 = Button(self, text = "  Calandar  ") 
     self.button1.grid(row=0, column = 0, sticky="ew") 

    #button2 
     self.button2 = Button(self, text = "  weather  ") 
     self.button2.grid(row=0, column = 1, sticky="ew") 

    #button3 
    #defult home menu in the future user sets a new home 
     self.button3 = Button(self, text = "  Whiteboard  ") 
     self.button3.grid(row=0, column = 2, sticky="ew") 

    #button4 
     self.button4 = Button(self, text = "  Internet  ") 
     self.button4.grid(row=0, column = 3, sticky="ew") 

    #button5 
     self.button5 = Button(self, text = "  Settings  ") 
     self.button5.grid(row=0, column = 4, sticky="ew") 
    #def settings(): 
     #pop up window where the settings options have wifi screen brightness  sleep options 
     #and also account settings 
     #One day also ui changes so color themes etc 

#Canvas 
#canvas ment to be part of whiteboard screen not a "pop-up" 
#canvas is ment to fill the whole page 


def xy(event): 
    global lastx, lasty 
    lastx, lasty = event.x, event.y 

def addLine(event): 
    global lastx, lasty 
    canvas.create_line((lastx, lasty, event.x, event.y)) 
    lastx, lasty = event.x, event.y 

root = Tk() 
root.columnconfigure(0, weight=1) 
root.rowconfigure(1, weight=1) 

canvas = Canvas(root) 
canvas.grid(column=0, row=1, sticky="nesw") 
canvas.bind("<Button-1>", xy) 
canvas.bind("<B1-Motion>", addLine) 





#endof canvas 
#root = Tk() 
root.title("Smartboard") 
#Geometry is ment to fit device???? 

root.geometry("530x500") 
app = Application(root) 

root.mainloop() 


# Copyright {2015} {Isaac Alderton} 

給出:

enter image description here

+0

現在我有它的問題,不會聽行或colums我需要在頂部王氏文本框是他們之前是如何在那裏畫布出現在同一窗口中:/如果你運行「破碎」的代碼,它顯示你的佈局應該如何......當我改變畫布到任何東西,但colum = 0它不工作..... –

+0

@IsaacAlderton完成。 – matsjoyce

+0

你先生很棒:) –