简体   繁体   中英

Drawing Labels on a Canvas using Tkinter

I'm trying to practice making a canvas using Tkinter but I'm having trouble making Labels appear on the canvas using a for loop. say I have a list that looks like this:

cities = ["Boston","New York","Chicago","Munich","Berlin","Monterrey"]

I want a for-loop that will go through each city and then place a label with the name of each city somewhere on the canvas. So far I have this code (only shows a canvas, can't get it to show me the labels):

class Map_Window(Frame):
    def __init__(self,title,left,top,width,height,master):
        Frame.__init__(self,master)
        self.master.title(title)
        self.master.geometry("%dx%d+%d+%d" % (width,height,left,top))
        self.create_gui()
        self.pack()
    def create_gui(self):
        self.frm1 = Frame(self)
        self.canvas = Canvas(self.frm1)
        self.canvas.pack()
        self.frm1.pack()

That code will set up the canvas. Now I'm trying to make the class that will place what's on the list inside the canvas. I have this so far:

class Controller:
    def __init__(self, canvas):
        self.canvas = canvas
    def draw_map(self,city_list):
        for l in city_list:
            lbl = Label(self.canvas,text = l).place(x = 1,y = 1)
            lbl.pack()

Main part of the code:

top_level = Tk()
win = Map_Window("Locations",150,150,300,400,top_level)
drawer = Controller(win.canvas)
drawer.draw_map(cities)
top_level.mainloop()

As I mentioned above, I can get the canvas to show up correctly but without the labels.

Based on your code snippet: lbl = Label(self.canvas,text = l).place(x = 1,y = 1) lbl.pack()

This should resume in an error like:

Traceback (most recent call last):
  File "C:\Users\User\SO_33382086.py", line 63, in 
    drawer.draw_map(cities)
  File "C:\Users\User\SO_33382086.py", line 58, in draw_map
    lbl.pack()
AttributeError: 'NoneType' object has no attribute 'pack'

To find out what is going wrong you can use:

[...]
def draw_map(self,city_list):
    for l in city_list:
        lbl = Label(self.canvas,text = l)
        print(lbl)
        print(lbl.place(x = 1,y = 1))
        # print(lbl.pack())
[...]

As shown in the snippet above, removing the pack command should solve your Issue.

Why? That's a simple question. place , grid and pack are geometry managers. They all place widgets on their own. Different behaviour, different parameters but all of them place widgets created by a constructor (eg lbl=Label(self.canvas, text=l) on another widget.

I assume you did not get any errors in front of your application. (MessageBox, etc.) But you have to get an error message like the one above on your console.

If you call the application using double click onto the script file (Windows OS) the command line output / interpreter shell will be available only as long as the script is running.

If you name your script file *.pyw, you will not get a visible interpreter shell at all. But then your complete window should refuse to show, not only the labels.

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