嗨,伙计们,
我是新来的。我正在准备一个用Python语言编写的图形用户界面,我需要在文本框中添加滚动条。我正在使用'Tkinter GRID'管理器来完成这个任务。附件是我所需要的文本框的图像。
如果我添加一个滚动条到文本框中,我会得到非常大的文本框。下面是我尝试过的代码,
请任何人帮帮我。
- TextBox1 = Entry(resultFrame)
- datFileText.grid(row=2, column=0, padx=20, ipadx=200, ipady=40)
- scrollBar1 = ScrolledText(TextBox1 )
- scrollBar1.grid(sticky=E)
提前谢谢你
马诺杰
附加图像
带有ScrollBar.jpg的TextBox
(6.3KB,1607浏览量)
# 回答1
你没有表现出什么
数据文件文本
就在你发布的代码中。下面是一个带有Tkinter.Text小部件和滚动条的Tkinter应用程序。Python2.7中的ScrolledText模块使用与Tkinter.Text相同的构造函数。
Tkinter.Entry小部件只能水平滚动。它需要一些额外的工作才能设置。这个
新墨西哥理工学院Tkinter 8.4参考资料
我可以教你怎么做。
- import Tkinter
- """
- Edit a file and save the text.
- """
-
- textFont1 = ("Courier New", 16, "normal")
-
- class ScrollbarX(Tkinter.Scrollbar):
- def set(self, low, high):
- if float(low) <= 0.0 and float(high) >= 1.0:
- self.grid_remove()
- else:
- self.grid()
- Tkinter.Scrollbar.set(self, low, high)
-
- class App(Tkinter.Tk):
- def __init__(self, fn, fnout):
- Tkinter.Tk.__init__(self)
- self.title("Text Widget")
- self.fin = open(fn, 'r')
- self.fnout = fnout
- self.mainFrame = Tkinter.Frame(self)
-
- top=self.winfo_toplevel()
- top.columnconfigure(0, weight=1)
- top.rowconfigure(0, weight=1)
-
- self.mainFrame.grid(row=0, column=0, sticky="nsew")
- self.exit = Tkinter.Button(self.mainFrame,
- text="Save and Exit",
- command=self.finish)
- self.exit.grid(row=0, column=0, sticky="ns")
-
-
- self.mainFrame.columnconfigure(0, weight=1)
- self.mainFrame.rowconfigure(1, weight=1)
-
- vscrollbar = ScrollbarX(self.mainFrame)
- vscrollbar.grid(row=1, column=1, sticky="ns")
- hscrollbar = ScrollbarX(self.mainFrame, orient=Tkinter.HORIZONTAL)
- hscrollbar.grid(row=2, column=0, sticky="ew")
-
- self.textWidget = Tkinter.Text(self.mainFrame,
- yscrollcommand=vscrollbar.set,
- xscrollcommand=hscrollbar.set,
- wrap=Tkinter.NONE,
- height=24,
- width=60,
- font=textFont1)
- self.textWidget.insert("1.0", self.fin.read())
- self.textWidget.grid(row=1, column=0, sticky="nsew")
-
- hscrollbar["command"] = self.textWidget.xview
- vscrollbar["command"] = self.textWidget.yview
-
- def finish(self):
- fout = open(self.fnout, 'w')
- fout.write(self.textWidget.get("1.0", "end"))
- fout.close()
- self.fin.close()
- app.destroy()
-
- if __name__ == "__main__":
- fn = "edit.txt"
- fnout = "editresult.txt"
- app = App(fn, fnout)
- app.mainloop()
# 回答2
如果添加PMW扩展并使用来自PMW的ScrolledText小部件,就会容易得多。
Http://www.ittc.ku.edu/~niehaus/clas.../scrolltext.py