【问题标题】:Connecting two files or classes in Python在 Python 中连接两个文件或类
【发布时间】:2020-01-31 14:11:12
【问题描述】:

我有两个页面(每个都有一个类)需要连接,page1(file1.py) 和 page2(file2.py)。

我想用一个将在 page1 上的按钮打开 page2。如前所述,每个页面的代码位于单独的文件 file1 和 file2 中。 我使用了 import file2 但只要我运行程序,它就会打开并运行我不想要的 file2.py。当用户需要打开 page2 时,我想通过按钮打开它。

有什么建议吗???

问候

希斯罗

【问题讨论】:

  • 你应该已经发布了你的代码示例。
  • 在 tkinter 中实现“页面”的方法有无数种。在不知道你是怎么做的情况下,我们只能猜测。请提供minimal reproducible example。

标签: python-3.x tkinter tkinter-entry


【解决方案1】:

只需将第二页放在class、import 中,然后在 function 中调用它。然后将function 作为command 提供给Button。

像这样:

Module_one:

import tkinter as tk


def test_func():
    import Module_two
    Module_two.TestClass()


root = tk.Tk()

b = tk.Button(root, text="Click", command=lambda: test_func())
b.pack()

root.mainloop()

模块_二:

class TestClass:
    def __init__(self):
        import tkinter as tk
        root = tk.Toplevel()
        lbl = tk.Label(root, text="Test Label")
        lbl.pack()

【讨论】:

  • 非常感谢。它几乎可以工作,但是当它调用 page2 时,它只显示 page2 的格言,没有应该在 page2 上的按钮。
  • @user3064089 你能再解释一下你的问题吗?因为我认为这不应该有任何问题。或者您可以通过编辑将您的代码放入您的问题中,因此我将解决其中的任何问题。 :)
猜你喜欢
  • 2023-03-03
  • 2023-03-10
  • 2022-11-17
  • 2018-10-10
  • 2021-04-17
  • 2020-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多