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

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

How can I import multiple Python modules into one Tkinter window

问题

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

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

  1. from tkinter import *
  2. class createWindowAndAddWidgets:
  3. def __init__(self, window):
  4. self.window = window
  5. def createWindow(self):
  6. window = Tk()
  7. window.title("窗口的标题")
  8. def addWidgets(self):
  9. myFrame = Frame(self.window, bg="#a1b8e1")
  10. myFrame.pack(side="top", fill="x")
  11. def showWindow(self):
  12. self.window.mainloop()

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

  1. from tkinter import *
  2. from file_to_create_window import createWindowAndAddWidgets
  3. class other_widgets:
  4. def the_other_widgets(self):
  5. myOtherFrame = Frame(self.window, bg="#1e8b1a")
  6. myOtherFrame.pack(side="bottom", fill="x")

主Python文件:

  1. from file_to_create_window import createWindowAndAddWidgets
  2. from file_to_create_other_widgets import other_widgets
  3. myWindow = createWindowAndAddWidgets(Tk())
  4. myWindow.createWindow()
  5. myWindow.addWidgets()
  6. myOtherWindow = other_widgets()
  7. myOtherWindow.the_other_widgets()
  8. 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:

  1. from tkinter import *
  2. class createWindowAndAddWidgets:
  3. def __init__(self, window):
  4. self.window = window
  5. def createWindow(self):
  6. window = Tk()
  7. window.title("Title of the window")
  8. def addWidgets(self):
  9. myFrame = Frame(self.window, bg="#a1b8e1")
  10. myFrame.pack(side="top", fill="x")
  11. def showWindow(self):
  12. self.window.mainloop()

Python file containing the other widgets

  1. from tkinter import *
  2. from file_to_create_window import createWindowAndAddWidgets
  3. class other_widgets:
  4. def the_other_widgets(self):
  5. myOtherFrame = Frame(self.window, bg="#1e8b1a")
  6. myOtherFrame.pack(side="bottom", fill="x")

Main Python file

  1. from file_to_create_window import createWindowAndAddWidgets
  2. from file_to_create_other_widgets import other_widgets
  3. myWindow = window()
  4. myWindow.createWindow()
  5. myWindow.addWidgets()
  6. myWindow.other_widgets()
  7. 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

  1. 编辑更新函数以防止重复窗口
  2. 你不能添加它们所有使用前缀 `window.`
  3. myWindow.createWindow()
  4. myWindow.addWidgets()
  5. myWindow.other_widgets()
  6. myWindow.showWindow()
  7. file_to_create_window.py
  8. class createWindowAndAddWidgets:
  9. def __init__(self, window):
  10. self.window = window
  11. self.createWindow()
  12. def createWindow(self):
  13. self.window = Tk()
  14. self.window.title("窗口的标题")
  15. self.addWidgets()
  16. def addWidgets(self):
  17. myFrame = Frame(self.window, bg="#a1b8e1")
  18. myFrame.pack(side="top", fill="x")
  19. self.showWindow()
  20. def showWindow(self):
  21. self.window.mainloop()
  22. file_to_create_window.py
  23. from file_to_create_window import createWindowAndAddWidgets
  24. import tkinter as tk
  25. myWindow = createWindowAndAddWidgets(tk)
  26. myWindow.createWindow()
英文:

Edited: Update function to prevent duplicated window.

You cannot add all of them. Use prefix window.

  1. myWindow.createWindow()
  2. myWindow.addWidgets()
  3. myWindow.other_widgets()
  4. myWindow.showWindow()

file_to_create_window.py

  1. class createWindowAndAddWidgets:
  2. def __init__(self, window):
  3. self.window = window
  4. self.createWindow()
  5. def createWindow(self):
  6. self.window = Tk()
  7. self.window.title("Title of the window")
  8. self.addWidgets()
  9. def addWidgets(self):
  10. myFrame = Frame(self.window, bg="#a1b8e1")
  11. myFrame.pack(side="top", fill="x")
  12. self.showWindow()
  13. def showWindow(self):
  14. self.window.mainloop()

file_to_create_window.py

  1. from file_to_create_window import createWindowAndAddWidgets
  2. import tkinter as tk
  3. myWindow = createWindowAndAddWidgets(tk)
  4. myWindow.createWindow()

答案2

得分: -1

Create an instance of the class before calling it.

For example:

  1. file_to_create_window 导入 createWindowAndAddWidgets
  2. file_to_create_other_widgets 导入 other_widgets
  3. myWindow = window()
  4. myWindow.createWindow()
  5. myWindow.addWidgets()
  6. otherWidgetsWindow = file_to_create_other_widgets()
  7. otherWidgetsWindow.other_widgets()
  8. myWindow.showWindow()
英文:

Create an instance of the class before calling it.

For example:

  1. from file_to_create_window import createWindowAndAddWidgets
  2. from file_to_create_other_widgets import other_widgets
  3. myWindow = window()
  4. myWindow.createWindow()
  5. myWindow.addWidgets()
  6. otherWidgetsWindow = file_to_create_other_widgets()
  7. otherWidgetsWindow.other_widgets()
  8. 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:

确定