简体   繁体   English

Tkinter中消息小部件的滚动条

[英]Scrollbar for Message Widget in Tkinter

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. 我正在尝试将以前在命令行中编写的游戏转换为GUI( https://github.com/abhinavdhere/Share-Trader-PC/releases/tag/v1.0 )我已经建立了一个类似于按钮结构的菜单在一个框架中,然后单击“帮助”,上一个框架f1应该消失并显示帮助文本。 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. 我提到了python和tkinter:在画布上使用滚动条并尝试这样做,但是它仍然仅显示消息,而没有滚动条。 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)

PS Why is it such a hassle to make a simple scrollbar? PS为什么制作一个简单的滚动条这么麻烦?

The message widget does not support scrolling. message小部件不支持滚动。 It is missing the commands yview and xview that are used for the scrolling protocol. 它缺少用于滚动协议的命令yviewxview 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. 您应该用text窗口小部件替换message窗口小部件,该text窗口小部件还显示多行文本,并可以支持使用标签滚动和格式化文本,以在需要时附加样式信息。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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