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

标签 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 最满意,并 promise 自己会改进。
  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()

最佳答案

终于找到了答案。根据我的发现(如果错误,请随意编辑),在 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()

希望这对大家有所帮助 - 如果您有任何疑问,请随时发表评论!

关于python - 如何使用 OOP 将小部件放置到 Tkinter 中的不同框架中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14865055/

相关文章:

python - 如何在 Python 的 Tkinter 中制作一个交互式列表,并包含可以编辑这些列表的按钮?

python - 将日期时间列的年份更改为另一列的年份 + 1

python - 错误 : too few arguments, 必须提供命令行包规范或 --file

Python 从文件中读取并保存到 utf-8

python - 打开文件 (Tkinter)

python-3.x - 在 tkinter 和 python 中嵌套网格和框架

python - RaspberryPi3 上的 WebDriverException : Message: invalid argument: can't kill an exited process with GeckoDriver, Selenium 和 Python

python - 填充 NaN 时'numpy.float6 4' object has no attribute ' fillna'

python-2.7 - Python 2.7,画中画 : "Failed building wheel for ..."

python - 如何按值对列表进行分组?