如何使用OOP将小部件放入Tkinter的不同框架中

时间:2013-02-13 23:11:39

标签: python python-2.7 tkinter

背景

大家好!我目前正在开发一个基本的GUI文本编辑器,可以加载和保存文本文件。我想在工具栏和文本框中使用多个框架,因为我学习here.我正在使用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窗口可见?

  4. 感谢大家的帮助!

    编辑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()
    

2 个答案:

答案 0 :(得分:3)

终于找到了答案。从我所发现的(如果它是错误的,可以随意编辑它)是只有两种方式可以在Tkinter中继承Frame:来自类本身和小部件当前所在的方法为了解决这个问题,我将类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()

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

答案 1 :(得分:1)

问题1:

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

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

是您应该使用的表格。

问题2:

假设您想要Applicationself中的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