我不能让画布按照我想要的方式工作

时间:2015-04-23 13:07:16

标签: python tkinter tkinter-canvas

我为我正在进行的设计创建了一个GUI,并在新窗口中弹出画布。我试图让它出现在按钮下面。

__author__ =  'Isaac Alderton'
__authorEmail__ =  'Isaacary92@gmail.com'
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}

1 个答案:

答案 0 :(得分:0)

您的问题是代码的最后一部分是:

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

您正在创建一个窗口,添加画布,然后创建一个新窗口,并将该帧添加到该新窗口。如果你注释掉第二个root = Tk(),它就可以了。将布局调整为:

__author__ =  'Isaac Alderton'
__authorEmail__ =  'Isaacary92@gmail.com'
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