如何将多个Python模块导入一个Tkinter窗口

huangapple go评论70阅读模式
英文:

How can I import multiple Python modules into one Tkinter window

问题

I understand your request. Here's the translated code:

Python文件,包含用于创建Tkinter窗口的模块:

from tkinter import *

class createWindowAndAddWidgets:
    def __init__(self, window):
        self.window = window

    def createWindow(self):
        window = Tk()
        window.title("窗口的标题")

    def addWidgets(self):
        myFrame = Frame(self.window, bg="#a1b8e1")
        myFrame.pack(side="top", fill="x")

    def showWindow(self):
        self.window.mainloop()

Python文件,包含其他小部件:

from tkinter import *
from file_to_create_window import createWindowAndAddWidgets

class other_widgets:
    def the_other_widgets(self):
        myOtherFrame = Frame(self.window, bg="#1e8b1a")
        myOtherFrame.pack(side="bottom", fill="x")

主Python文件:

from file_to_create_window import createWindowAndAddWidgets
from file_to_create_other_widgets import other_widgets

myWindow = createWindowAndAddWidgets(Tk())
myWindow.createWindow()
myWindow.addWidgets()

myOtherWindow = other_widgets()
myOtherWindow.the_other_widgets()

myWindow.showWindow()

这是翻译好的代码部分,没有其他内容。

英文:

How can I import multiple Python modules which contain different parts of a Tkinter window.

I have created a Tkinter window inside a Python class. This Python class also contains multiple different Tkinter widgets. I have then imported the module into my main Python file and run the code. This has worked as expected and the Tkinter window was correctly displayed.

I then imported a Python module from inside a different Python file. When I run this code, I receive the error message: type object 'window' has no attribute 'tk'.

I understand that this is because the new Python file cannot find the Tkinter window. However, I am unable to fix this issue.

If possible, I do not want to create the Tkinter window in the main Python file. I also do not want to add both classes to the same Python file, in order to keep my code clean.

An example of my code:

Python file containing the module to create the Tkinter window:

from tkinter import *

class createWindowAndAddWidgets:
    def __init__(self, window):
        self.window = window

    def createWindow(self):
        window = Tk()
        window.title("Title of the window")

def addWidgets(self):
    myFrame = Frame(self.window, bg="#a1b8e1")
    myFrame.pack(side="top", fill="x")

def showWindow(self):
    self.window.mainloop()

Python file containing the other widgets

from tkinter import *
from file_to_create_window import createWindowAndAddWidgets

class other_widgets:
    def the_other_widgets(self):
        myOtherFrame = Frame(self.window, bg="#1e8b1a")
        myOtherFrame.pack(side="bottom", fill="x")

Main Python file

from file_to_create_window import createWindowAndAddWidgets
from file_to_create_other_widgets import other_widgets

myWindow = window()
myWindow.createWindow()
myWindow.addWidgets()
myWindow.other_widgets()
myWindow.showWindow()

As you can see from the above example, I have 3 Python files. The first file contains a class which creates the window and adds some widgets to the window. The second file contains a class which adds other widgets to the window. The third file is supposed to run the classes and produce the output. However, I receive the error message: type object 'window' has no attribute 'tk'. I recieve this error on Line 6 of the Python fle contining the other widgets: myOtherFrame = Frame(self.window, bg="#1e8b1a")

答案1

得分: -1

编辑更新函数以防止重复窗口

你不能添加它们所有使用前缀 `window.`

    myWindow.createWindow()
    myWindow.addWidgets()
    myWindow.other_widgets()
    myWindow.showWindow()


file_to_create_window.py

    class createWindowAndAddWidgets:
        def __init__(self, window):
            self.window = window         
            self.createWindow()
             
    
        def createWindow(self):
            self.window = Tk()
            self.window.title("窗口的标题")
            self.addWidgets()
             
            
    
        def addWidgets(self):
            myFrame = Frame(self.window, bg="#a1b8e1")
            myFrame.pack(side="top", fill="x")
            self.showWindow()
    
        def showWindow(self):
            self.window.mainloop()




file_to_create_window.py

    from file_to_create_window import createWindowAndAddWidgets
    import tkinter as tk
     
    
    myWindow = createWindowAndAddWidgets(tk)
    myWindow.createWindow()
英文:

Edited: Update function to prevent duplicated window.

You cannot add all of them. Use prefix window.

myWindow.createWindow()
myWindow.addWidgets()
myWindow.other_widgets()
myWindow.showWindow()

file_to_create_window.py

class createWindowAndAddWidgets:
    def __init__(self, window):
        self.window = window         
        self.createWindow()
         

    def createWindow(self):
        self.window = Tk()
        self.window.title("Title of the window")
        self.addWidgets()
         
        

    def addWidgets(self):
        myFrame = Frame(self.window, bg="#a1b8e1")
        myFrame.pack(side="top", fill="x")
        self.showWindow()

    def showWindow(self):
        self.window.mainloop()

file_to_create_window.py

from file_to_create_window import createWindowAndAddWidgets
import tkinter as tk
 

myWindow = createWindowAndAddWidgets(tk)
myWindow.createWindow()

答案2

得分: -1

Create an instance of the class before calling it.

For example:

从 file_to_create_window 导入 createWindowAndAddWidgets
从 file_to_create_other_widgets 导入 other_widgets

myWindow = window()
myWindow.createWindow()
myWindow.addWidgets()

otherWidgetsWindow = file_to_create_other_widgets()
otherWidgetsWindow.other_widgets()
myWindow.showWindow()
英文:

Create an instance of the class before calling it.

For example:

from file_to_create_window import createWindowAndAddWidgets
from file_to_create_other_widgets import other_widgets

myWindow = window()
myWindow.createWindow()
myWindow.addWidgets()

otherWidgetsWindow = file_to_create_other_widgets()
otherWidgetsWindow.other_widgets()
myWindow.showWindow()

huangapple
  • 本文由 发表于 2023年5月21日 17:30:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定