2

背景

大家好!我目前正在开发一个可以加载和保存文本文件的基本 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)

问题

  1. 仍然使用不同的方法,我怎样才能确保每个小部件都放置在正确的框架中?

    • 如果这是不可能的,请告诉我如何使用 OOP 来做到这一点——我对 Tkinter 在那种环境下最满意,并承诺自己会改进。
  2. 解释你的答案。我需要同源——而不是简单地对着电脑点头并继续前进。

  3. 额外的信用:我将如何在 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()
4

2 回答 2

3

终于找到了答案。根据我的发现(如果错误,请随时编辑)Frame在 Tkinter 中只有两种方法可以继承 a:从类本身和从小部件当前所在的方法。解决问题,我将类设置Application为框架,然后在其中放置其他框架。这是我所做的基本演绎:

#Import Tkinter
from Tkinter import *

#Main Frame
class Application(Frame):
    def __init__(self, master):  #initialize the grid and widgets
        Frame.__init__(self,master)
        self.grid()
        self.redFUN() #initialize the red frame's Function
        self.greenFUN() #initialize the green frame's Function
        self.widgets() #To show that you can still place non-Frame widgets 
    def widgets(self):
        self.mylabel = Label (self, text = "Hello World!")
        self.mylabel.grid()
    def redFUN(self): #The 'self' means that it is an instance of the main frame
        #Init the red frame
        self.redFrame = Frame(root, width = 100, height = 50,pady = 5,
                              bg = "red")
        self.redFrame.grid()



    def greenFUN(self): #Child of the mainframe
        self.greenFrame = Frame(root, width = 100, height = 50,pady = 5,
                          bg = "green") #it is green!
        self.greenFrame.grid()








#These lines of code are used for the grid
root = Tk()
root.title("Frame Example")
root.geometry("300x300")
app = Application(root)

root.mainloop()

我希望这对每个人都有帮助-如果您有任何疑问,请随时发表评论!

于 2013-02-16T17:24:18.683 回答
1

问题1:

一个小部件只能有一个直接父级。传递两个父母没有语法。例如,您似乎self同时self.toolbar作为self.saveButton.

myButton = Button(self.toolbar, text="Blah", command=self.someCommand)

是您应该使用的表格。

问题2:

假设您希望Application(AKA the selfin Button(self, self.toolbar...)) 成为myButton. 这也不起作用,因为要成为 Tk 小部件的分层父级,类也必须是Widget. 通常,如果您希望这样做是继承tk.Tk()如下Application

class Application(Tk):

    def __init__(self, *args, **kwargs):


        Tk.__init__(self, *args, **kwargs) #It's important that you call the parent class's __init__ method first

        self.createWidgets()

    def createWidgets(self):

        self.myButton = Button(self, text="Blah", command=lambda x: print "x")
        #this is ok, because now self (AKA Application) is a valid instance of Tk
于 2013-02-14T01:24:03.067 回答