在Tkinter Python中向文本框添加滚动条

嗨,伙计们,
我是新来的。我正在准备一个用Python语言编写的图形用户界面,我需要在文本框中添加滚动条。我正在使用'Tkinter GRID'管理器来完成这个任务。附件是我所需要的文本框的图像。
如果我添加一个滚动条到文本框中,我会得到非常大的文本框。下面是我尝试过的代码,
请任何人帮帮我。

选择 | 换行 | 行号
  1. TextBox1 = Entry(resultFrame)
  2. datFileText.grid(row=2, column=0, padx=20, ipadx=200, ipady=40)
  3. scrollBar1 = ScrolledText(TextBox1 )
  4. scrollBar1.grid(sticky=E)

提前谢谢你
马诺杰
附加图像
File Type: jpg
带有ScrollBar.jpg的TextBox
(6.3KB,1607浏览量)

# 回答1


你没有表现出什么
数据文件文本
就在你发布的代码中。下面是一个带有Tkinter.Text小部件和滚动条的Tkinter应用程序。Python2.7中的ScrolledText模块使用与Tkinter.Text相同的构造函数。
Tkinter.Entry小部件只能水平滚动。它需要一些额外的工作才能设置。这个
新墨西哥理工学院Tkinter 8.4参考资料
我可以教你怎么做。

选择 | 换行 | 行号
  1. import Tkinter
  2. """
  3. Edit a file and save the text.
  4. """
  5.  
  6. textFont1 = ("Courier New", 16, "normal")
  7.  
  8. class ScrollbarX(Tkinter.Scrollbar):
  9.     def set(self, low, high):
  10.         if float(low) <= 0.0 and float(high) >= 1.0:
  11.             self.grid_remove()
  12.         else:
  13.             self.grid()
  14.         Tkinter.Scrollbar.set(self, low, high)
  15.  
  16. class App(Tkinter.Tk):
  17.     def __init__(self, fn, fnout):
  18.         Tkinter.Tk.__init__(self)
  19.         self.title("Text Widget")
  20.         self.fin = open(fn, 'r')
  21.         self.fnout = fnout
  22.         self.mainFrame = Tkinter.Frame(self)
  23.  
  24.         top=self.winfo_toplevel()
  25.         top.columnconfigure(0, weight=1)
  26.         top.rowconfigure(0, weight=1)
  27.  
  28.         self.mainFrame.grid(row=0, column=0, sticky="nsew")
  29.         self.exit = Tkinter.Button(self.mainFrame,
  30.                                    text="Save and Exit",
  31.                                    command=self.finish)
  32.         self.exit.grid(row=0, column=0, sticky="ns")
  33.  
  34.  
  35.         self.mainFrame.columnconfigure(0, weight=1)
  36.         self.mainFrame.rowconfigure(1, weight=1)
  37.  
  38.         vscrollbar = ScrollbarX(self.mainFrame)
  39.         vscrollbar.grid(row=1, column=1, sticky="ns")
  40.         hscrollbar = ScrollbarX(self.mainFrame, orient=Tkinter.HORIZONTAL)
  41.         hscrollbar.grid(row=2, column=0, sticky="ew")
  42.  
  43.         self.textWidget = Tkinter.Text(self.mainFrame,
  44.                                        yscrollcommand=vscrollbar.set,
  45.                                        xscrollcommand=hscrollbar.set,
  46.                                        wrap=Tkinter.NONE,
  47.                                        height=24,
  48.                                        width=60,
  49.                                        font=textFont1)
  50.         self.textWidget.insert("1.0", self.fin.read())
  51.         self.textWidget.grid(row=1, column=0, sticky="nsew")
  52.  
  53.         hscrollbar["command"] = self.textWidget.xview
  54.         vscrollbar["command"] = self.textWidget.yview
  55.  
  56.     def finish(self):
  57.         fout = open(self.fnout, 'w')
  58.         fout.write(self.textWidget.get("1.0", "end"))
  59.         fout.close()
  60.         self.fin.close()
  61.         app.destroy()
  62.  
  63. if __name__ == "__main__":
  64.     fn = "edit.txt"
  65.     fnout = "editresult.txt"
  66.     app = App(fn, fnout)
  67.     app.mainloop()
# 回答2


如果添加PMW扩展并使用来自PMW的ScrolledText小部件,就会容易得多。
Http://www.ittc.ku.edu/~niehaus/clas.../scrolltext.py

标签: python

添加新评论