背景
大家好!我目前正在开发一个可以加载和保存文本文件的基本 GUI 文本编辑器。正如我在这里学到的那样,我想为工具栏和文本框使用多个框架。我正在使用 OOP,并在方法中设置了我的框架__init__
,并在方法中设置了小部件widget
。由于某种原因,小部件无法放置在它们各自的框架中。
代码
from Tkinter import *
class Application:
def __init__(self,parent): #initialize the grid and widgets
self.myParent = parent
#Init the toolbar
self.toolbar = Frame(parent)
self.toolbar.grid(row = 0)
#Init frame for the text box
self.mainframe = Frame(parent)
self.toolbar.grid(row = 1)
def widget(self):#Place widgets here
#Save Button
self.saveButton = Button (self, self.toolbar,
text = "Save", command = self.saveMe)
self.saveButton.grid(column = 0, row = 0, sticky = W)
#Open Button
self.openButton = Button (self, self.toolbar,
text = "Open", command = self.openMe)
self.openButton.grid(column = 0, row = 1, sticky = W)
#Area where you write
self.text = Text (self, self.mainframe,
width = (root.winfo_screenwidth() - 20),
height = (root.winfo_screenheight() - 10))
self.text.grid(row = 2)
问题
仍然使用不同的方法,我怎样才能确保每个小部件都放置在正确的框架中?
- 如果这是不可能的,请告诉我如何使用 OOP 来做到这一点——我对 Tkinter 在那种环境下最满意,并承诺自己会改进。
请解释你的答案。我需要同源——而不是简单地对着电脑点头并继续前进。
额外的信用:我将如何在 OOP 中使用 Tkinter 初始化多个窗口(每个窗口是不同的类)?例如,如果这是我的代码:
class MainWindow(Frame): ---init stuff--- def widget(self): newWindow = Button(self, text = "click for a new window", command = self.window) newWindow.grid() def window(self): #What would I put in here to initialize the new window?? class theNextWindow(Frame):
我会在
window.self
方法中放入什么使theNextWindow
窗口可见?
感谢大家的帮助!
编辑 1
self.widget()
我在方法中添加了这一行__init__
,我得到了这个“精彩”错误的奖励:
Traceback (most recent call last):
File "D:\Python Programs\Text Editor\MyTextv2.py", line 67, in <module>
app = Application(root)
File "D:\Python Programs\Text Editor\MyTextv2.py", line 14, in __init__
self.widget()
File "D:\Python Programs\Text Editor\MyTextv2.py", line 24, in widget
text = "Save", command = self.saveMe)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2044, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1965, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1943, in _setup
self.tk = master.tk
AttributeError: Application instance has no attribute 'tk'
由于错误日志在这里清楚地引用了我的主循环:File "D:\Python Programs\Text Editor\MyTextv2.py", line 67, in <module>
app = Application(root)
我决定添加它:
root = Tk()
root.title("My Text Editor")
#This is wierd - it gets the computer windows dimensions
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(0)
#And then applies them here
root.geometry("%dx%d+0+0" % (w, h))
app = Application(root)
root.mainloop()