简体   繁体   中英

just a canvas in a tkinter window

If I try to place a canvas in a tkinter window and nothing else with this code:

from tkinter import ttk
from tkinter import *
from tkinter.ttk import *
class Application(Frame):
    def createWidgets(self):
        self.can = Canvas(self.master, width=500, height=250)
        self.can.grid(row=2, column=1)
        self.can.create_line(0,0,500,200)
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()

a window is never created. I found that adding a button to create a canvas works:

from tkinter import ttk
from tkinter import *
from tkinter.ttk import *
class Application(Frame):
    def makecanvas(self):
        self.grid_forget()
        self.can = Canvas(self.master, width=500, height=250)
        self.can.grid(row=2, column=1)
        self.can.create_line(0,0,500,200)
    def createWidgets(self):
        self.inst = Button(self)
        self.inst["text"] = "GO!"
        self.inst["command"] =  self.makecanvas           
        self.inst.grid(row=3, column=1, pady=15, sticky=N)
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()

Also, If I comment out the create canvas function, the button I removed with self.grid_forget() doesn't disappear. Is there a better way to do this?

The problem is that you are mixing geometry managers in the same window. You can only use one within a given parent widget. You can use both within your application as a whole, but you are using them both on widgets that have the same parent.

You need to rewrite your code to use only grid, or only pack, for all widgets directly under the root window.

Using the grid geometry manager

from tkinter import ttk
from tkinter import *
from tkinter.ttk import *


class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()      
        self.createWidgets()

    def createWidgets(self):
        self.can = Canvas(self.master, width=500, height=250)
        self.can.grid(row=2, column=1)
        self.can.create_line(0,0,500,200)

root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()

Or using the pack geometry manager

from tkinter import ttk
from tkinter import *
from tkinter.ttk import *


class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

    def createWidgets(self):
        self.can = Canvas(self.master, width=500, height=250)
        self.can.pack()
        self.can.create_line(0,0,500,200)

root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()

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