简体   繁体   中英

tkinter canvas text output

I have been looking at my code for a while and new to tkinter. The purpose of my code is to display text within the Canvas widget not overlay a label. But unsure how to do this:

My code is as follows:

from tkinter import *

class Example(Frame):

    def printLabel(self):
        self.hello = []
        self.hello.append('Hello')
        self.hello.append('World!')
        return(self.hello)

    def updatePanel(self):
        self.panelA.config(text="{}".format(self.printLabel()))

    def __init__(self, root):
        Frame.__init__(self, root)
        self.buttonA()
        self.viewingPanel()

    def buttonA(self):
        self.firstPage = Button(self, text="Print Text", bd=1, anchor=CENTER, height = 11, width = 13, command=lambda: self.updatePanel())
        self.firstPage.place(x=0, y=0)

    def viewingPanel(self):
        self.panelA = Label(self, bg='white', width=65, height=13, padx=3, pady=3, anchor=NW, text="")
        self.panelA.place(x=100, y=0)
        self.cl= Canvas(self.panelA,bg='WHITE',width=165,height=113,relief=SUNKEN)
        canvas_id = self.cl.create_text(15, 15, anchor="nw")

        self.xb= Scrollbar(self.panelA,orient="horizontal", command=self.cl.xview)
        self.xb.pack(side=BOTTOM,fill=X)
        self.xb.config(command=self.cl.xview)
        self.yb= Scrollbar(self.panelA,orient="vertical", command=self.cl.yview)
        self.yb.pack(side=RIGHT,fill=Y)
        self.yb.config(command=self.cl.yview)

        self.cl.itemconfig(canvas_id,font=('Consolas',9), text="{}".format(self.printLabel()))
        self.cl.configure(scrollregion = self.cl.bbox("all"))
        self.cl.config(xscrollcommand=self.xb.set, yscrollcommand=self.yb.set)
        self.cl.config(width=250,height=150)
        self.cl.pack(side=LEFT,expand=True,fill=BOTH)

def main():
    root = Tk()
    root.title("Tk")
    root.geometry('378x176')
    app = Example(root)
    app.pack(expand=True, fill=BOTH)
    root.mainloop()

if __name__ == '__main__':
    main()

The Hello World! should display without the brackets in the Canvas but the main issue is that when I click on the Button it just overlaps the canvas and prints out the append on to the Label .

The Label should be inside the Canvas .

Here's how to fix the "main issue" along with the "brackets issue". The latter is taken care of by using the string join() method as suggested in the comments.

The updatePanel() method has been modified so it first create a Label widget with the text you want displayed in it, followed by a Canvas "window" object specifying that widget as its contents. Code for the way you were attempting to do it was also removed from the other class methods.

from tkinter import *


class Example(Frame):

    def __init__(self, root):
        Frame.__init__(self, root)
        self.buttonA()
        self.viewingPanel()

    def printLabel(self):
        text = []
        text.append('Hello')
        text.append('World!')
        return ' '.join(text)

    def updatePanel(self):
        label = Label(self, bg='white', padx=3, pady=3, anchor=NW,
                      text=self.printLabel())
        label.place(relx=0.5, rely=0.5, anchor=CENTER)
        self.cl.create_window(100, 100, window=label)  # Put Label in a Canvas "window".

    def buttonA(self):
        self.firstPage = Button(self, text="Print Text", bd=1, anchor=CENTER, height=11,
                                width=13, command=lambda: self.updatePanel())
        self.firstPage.place(x=0, y=0)

    def viewingPanel(self):
        self.panelA = Label(self, bg='white', width=65, height=13, padx=3, pady=3,
                            anchor=NW, text="")
        self.panelA.place(x=100, y=0)
        self.cl= Canvas(self.panelA, bg='WHITE', width=165, height=113, relief=SUNKEN)
        canvas_id = self.cl.create_text(15, 15, anchor="nw")

        self.xb= Scrollbar(self.panelA,orient="horizontal", command=self.cl.xview)
        self.xb.pack(side=BOTTOM, fill=X)
        self.xb.config(command=self.cl.xview)
        self.yb= Scrollbar(self.panelA, orient="vertical", command=self.cl.yview)
        self.yb.pack(side=RIGHT, fill=Y)
        self.yb.config(command=self.cl.yview)

        self.cl.itemconfig(canvas_id, font=('Consolas',9), text=self.printLabel())
        self.cl.configure(scrollregion=self.cl.bbox("all"))
        self.cl.config(xscrollcommand=self.xb.set, yscrollcommand=self.yb.set)
        self.cl.config(width=250, height=150)
        self.cl.pack(side=LEFT, expand=True, fill=BOTH)


def main():
    root = Tk()
    root.title("Tk")
    root.geometry('378x176')
    app = Example(root)
    app.pack(expand=True, fill=BOTH)
    root.mainloop()

if __name__ == '__main__':
    main()

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