如何解决如何在Python中的标签或其他小部件中设置文本格式?
所以我有一个变量,它的值很长。该字符串将在新窗口中显示给用户,以便他们可以复制它并将其粘贴到程序中。因此,变量的值显示在弹出的新窗口的标签中。问题是,变量的文本全部水平且在一行中,因此它从窗口屏幕上消失了,这看起来非常烦人,并且使复制变得更加困难。有什么办法可以格式化它,使其在窗口上带有滚动条垂直运行?
解决方法
我发现使用文本框效果更好-以下是这些网站: http://effbot.org/tkinterbook/text.htm
http://effbot.org/zone/tkinter-scrollbar-patterns.htm
这是我使用的代码:
from tkinter import * #Import tkinter module
import csv #Import csv module
from tkinter import messagebox
# These lines define the main/parent window
root = Tk()
root.title("Main Window")
root.geometry("500x500")
root.configure(bg='white')
def NewwindowButton():
# The following three lines define the sub-window
info_window = Toplevel(root)
info_window.title("This is the info window")
info_window.geometry("300x400")
# These lines define a frame inside the window,# and the text box inside that frame with the
# 'variable_with_text' variable supplying the text.
variable_with_text = "Here is the text..."
info_window_frame = Frame(info_window,bg="white")
info_window_frame.place(relwidth=1.0,relheight=1.0)
text_widget = Text(info_window,bg="white")
text_widget.place(relwidth=1.0,relheight=1.0)
text_widget.insert(INSERT,variable_with_text)
# These lines define a scrollbar for the text window
text_scrollbar = Scrollbar(info_window)
text_scrollbar.pack(side=RIGHT,fill=Y)
# These lines link the text widget and scrollbar together
text_widget.config(yscrollcommand=text_scrollbar.set)
text_scrollbar.config(command=text_widget.yview)
new_window_button = Button(root,text="Open new window",command=NewwindowButton,bg="grey")
new_window_button.pack()
root.mainloop()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。