简体   繁体   中英

Tkinter canvas not showing up

I'm quite new to Tkinter and this is my first program, can anyone tell me why the canvas is not showing up? I'm not getting any errors so I assume it is working but just not visible? I tried moving it up a layer but it was still invisible. Here is my code:

from Tkinter import *
import Tkinter as tk

class Application(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.frame = Frame(self.master)
        self.master = master
        self.window()
        self.drawFigure()
        # self.master.attributes('-fullscreen', True)
        self.master.bind("<Escape>", self.end_fullscreen)

    def window(self):
        self.frame = Frame(self.master)
        screen_width = self.frame.winfo_screenwidth() / 2
        screen_height = self.frame.winfo_screenheight() / 2
        self.master.geometry('%dx%d' % (screen_width, screen_height))

    def end_fullscreen(self, event=None):
        self.master.attributes("-fullscreen", False)

    def drawFigure(self):
        self.frame = Frame(self.master)
        self.C = Canvas(self.frame, width=200, height=200, bg = 'red')
        self.C.pack()
        self.C.create_rectangle(50, 20, 150, 80, fill="#476042")

if __name__ == '__main__':
    root = tk.Tk()
    w = Application(root)
    w.master.mainloop()

Appreciate all the input.

You forgot to call pack() on the Frame you created in drawFigure() :

def drawFigure(self):
    self.frame = Frame(self.master)
    self.frame.pack()                      # <--- There
    self.C = Canvas(self.frame, width=200, height=200, bg = 'red')
    self.C.pack()
    self.C.create_rectangle(50, 20, 150, 80, fill="#476042")

You're creating three sub frames of your parent, storing each of them as self.frame (so all but the last one are lost), and not placing any of them anywhere.

So, you've correctly placed the canvas on one of these invisible frames, but that doesn't do any good.

I'm not sure what you're trying to do with all these separate frames.

  • If you really need three sibling frames, you have to store them in separate variables, or in a list, or something, and you need to place them.
  • If you need one sibling frame, just create it once instead of three times, and again, you need to place it.
  • If you need three or one child frames instead of sibling frames, create them with self rather than self.master .
  • If you don't need any sibling or child frames at all, don't create them, and just place the canvas on self instead of self.frame .

You import Tkinter and Tkinter as tk, which gets confusing.

Application inherits from Frame so you don't have to create additional frames inside. Certainly not more than one named self.frame .

How about this:

from Tkinter import *

class Application(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.master = master
        self.pack()
        self.window()
        self.drawFigure()
        self.master.bind("<Escape>", self.end_fullscreen)

    def window(self):
        screen_width = self.winfo_screenwidth() / 2
        screen_height = self.winfo_screenheight() / 2
        self.master.geometry('%dx%d' % (screen_width, screen_height))

    def end_fullscreen(self, event=None):
        self.master.attributes("-fullscreen", False)

    def drawFigure(self):
        self.C = Canvas(self, width=200, height=200, bg = 'red')
        self.C.pack()
        self.C.create_rectangle(50, 20, 150, 80, fill="#476042")

if __name__ == '__main__':
    root = Tk()
    w = Application(root)
    w.master.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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