Scrollbar for Message Widget in Tkinter

5.6k views Asked by At

I am trying to convert a game I had previously written in command line into GUI (https://github.com/abhinavdhere/Share-Trader-PC/releases/tag/v1.0) I have built a menu like structure of buttons in one frame, and then on clicking help, the previous frame f1 should disappear and help text should be displayed. I used a Message widget to display the text but it is long and needs scrollbar. I tried to add a vertical scrollbar but couldn't make it work. I referred python and tkinter: using scrollbars on a canvas and tried to do it that way but it still displays only the Message but no scrollbar. Here is the function for it:

def help(self):
    self.f1.pack_forget()
    f2=tk.Frame(self,bg='#FFCC00')
    f2.grid(row=0,column=0)
    helpMan=open("Game Rules.txt","r")
    hText=helpMan.read()
    c1=tk.Canvas(f2,width=640,height=480,scrollregion=(0,0,700,500))
    c1.pack(side="left",expand=True,fill="both")
    text1=tk.Message(f2,text=hText)
    c1.create_window(0,0,anchor="nw",window=text1)
    scrollY=tk.Scrollbar(f2,orient="vertical",command=c1.yview)
    scrollY.pack(side="right",fill="y") 
    c1.config(yscrollcommand = scrollY.set)

P.S. Why is it such a hassle to make a simple scrollbar?

1

There are 1 answers

1
patthoyts On BEST ANSWER

The message widget does not support scrolling. It is missing the commands yview and xview that are used for the scrolling protocol. It is really just a multiline label. It is also ugly and can't be themed.

You should replace the message widget with a text widget which also displays multiline text and can support scrolling and formatted text using tags to attach styling information if required.

To make the text widget look the same as the Message widget the following should work:

m = Message(root)
txt = Text(root, background=m.cget("background"), relief="flat",
    borderwidth=0, font=m.cget("font"), state="disabled")
m.destroy()