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

Python 如何使用OOP将小部件放置到Tkinter中的不同框架中,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,背景 大家好!我目前正在开发一个基本的GUI文本编辑器,它可以加载和保存文本文件。我想为工具栏和文本框使用多个框架,因为我了解到我正在使用OOP,并且已经在uu init_u_u方法中设置了我的框架,并且在小部件方法中设置了小部件。由于某些原因,小部件无法放置在各自的框架内 代码 问题 仍然使用不同的方法,如何确保每个小部件都放置在正确的框架中 如果这是不可能的,请告诉我如何使用OOP实现它-我对Tkinter在这种环境下的表现最为满意,并承诺自己会改进。 请解释你的答案。我需要同源——而不是简

背景

大家好!我目前正在开发一个基本的GUI文本编辑器,它可以加载和保存文本文件。我想为工具栏和文本框使用多个框架,因为我了解到我正在使用OOP,并且已经在uu init_u_u方法中设置了我的框架,并且在小部件方法中设置了小部件。由于某些原因,小部件无法放置在各自的框架内

代码

问题

仍然使用不同的方法,如何确保每个小部件都放置在正确的框架中

如果这是不可能的,请告诉我如何使用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方法中添加什么使TheNext窗口可见

谢谢大家的帮助

编辑1

我在_init__方法中添加了line self.widget,我得到了这个奇妙的错误:

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'
问题1:

小部件只能有一个直接父级。没有语法来传递双亲。例如,您似乎同时将self和self.toolbar作为self.saveButton的父对象传入

myButton = Button(self.toolbar, text="Blah", command=self.someCommand)
这是你应该使用的表格

问题2:

假设您希望应用程序在Buttonself、self.toolbar中使用self。。。成为myButton的父级。这也行不通,因为要成为Tk小部件的层次父级,类还必须是小部件的实例。通常,如果希望这样做,您要做的是在应用程序中继承tk.tk,如下所示:

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
问题1:

小部件只能有一个直接父级。没有语法来传递双亲。例如,您似乎同时将self和self.toolbar作为self.saveButton的父对象传入

myButton = Button(self.toolbar, text="Blah", command=self.someCommand)
这是你应该使用的表格

问题2:

假设您希望应用程序在Buttonself、self.toolbar中使用self。。。成为myButton的父级。这也行不通,因为要成为Tk小部件的层次父级,类还必须是小部件的实例。通常,如果希望这样做,您要做的是在应用程序中继承tk.tk,如下所示:

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
我终于找到了答案。从我发现的情况来看,如果框架在Tkinter中继承的方式有两种,请随意编辑:从类本身和小部件当前所在的方法。为了解决这个问题,我将类应用程序设置为一个框架,然后在其中放置其他框架。以下是我所做工作的基本表现:

#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()
我希望这对每个人都有帮助-如果你有任何问题,请随时发表评论

我终于找到了答案。从我发现的情况来看,如果框架在Tkinter中继承的方式有两种,请随意编辑:从类本身和小部件当前所在的方法。为了解决这个问题,我将类应用程序设置为一个框架,然后在其中放置其他框架。以下是我所做工作的基本表现:

#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()

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

你现在的东西怎么了?如果代码生成错误,请在问题中包含错误文本。另外,就风格而言,widget并不是一个很好的方法名称。方法做事情,一个好的方法名指示方法做什么。小部件的更好名称是setUpWidgets或setUpWindow。代码不会生成错误,这是一个逻辑问题。窗户是空的,灰色的——显然没有多大帮助!在您的代码中,您从不调用.widget,因此该方法中的任何代码都不会执行。运行这些东西的代码在哪里?@JoelCornett谢谢你的建议。我添加了它,我得到了一个很好的错误信息。。。还有,为什么你认为widget不是一个好名字?注:这是一个n00b问题:如何在Windows7上进行代码引用?您现在的代码有什么问题?如果代码生成错误,请在问题中包含错误文本。另外,就风格而言,widget并不是一个很好的方法名称。方法做事情,一个好的方法名指示方法做什么。小部件的更好名称是setUpWidgets或setUpWindow。代码不会生成错误,这是一个逻辑问题。窗户是空的,灰色的——显然没有多大帮助!在您的代码中,您从不调用.widget,因此该方法中的任何代码都不会执行。运行这些东西的代码在哪里?@JoelCornett谢谢你的建议。我添加了它,我得到了一个很好的错误信息。。。还有,为什么你认为widget不是一个好名字?PS:这是一个n00b问题:我该怎么办
Windows 7上的代码引号?您在注释中写道“self”表示它是主框架的子框架。这是不正确的。self意味着它是一个实例方法——属于类的实例而不是类的实例的方法。“孩子”这个词不适用于这种情况。这里有没有错误的信息?1的原因是什么?当否决投票时,请解释原因并提出改进的理由。你在评论中写道“自我”意味着它是主体框架的孩子。这是不正确的。self意味着它是一个实例方法——属于类的实例而不是类的实例的方法。“孩子”这个词不适用于这种情况。这里有没有错误的信息?投-1票的原因是什么?当投反对票时,请解释原因并提出改进的理由。