【问题标题】:imported python file wont return variable from class to original file导入的python文件不会将变量从类返回到原始文件
【发布时间】:2019-04-12 02:22:19
【问题描述】:

我希望代码导入一个 python 文件,该文件显示一个带有 2 个按钮的 tkinter 窗口。根据用户按下的按钮,它会运行一个函数,该函数应该将某个字符串返回到原始 python 文件。我之前已经通过将输入框的内容返回到原始文件来完成此操作,但是对于这项任务,我没有得到任何线索,因为代码对我来说有意义并且没有产生错误?

原始python代码:

def menu():
    import Menu_window
    Login_or_create= Menu_window.start()
    print(Login_or_create)

    if Login_or_create == "Login":
        print("Logged in")

    if Login_or_create == "Create":
        print("Creating user")

名为 Menu_Window.py 的导入文件:

from tkinter import *
from tkinter import ttk

class Menu_window():
    def __init__(self,window):
        self.window = window
        self.window.title("Menu")

        bottom_frame=LabelFrame(self.window)
        bottom_frame.grid(row=2)

        Login= Label(text = "Hello, Please login or create an account to play!")
        Login.grid(row=0)

        ttk.Button(bottom_frame,text = 'Login',command = returN()).grid(row=0)
        ttk.Button(bottom_frame,text = 'Create account',command = returN2()).grid(row=0,column=1)

def returN():
    return "Login"

def returN2():
    return "Create"

def start():
    window=Tk()
    Login_or_Create=Menu_window(window)
    window.mainloop()
    window.destory()

【问题讨论】:

  • returN()returN2() 在这里什么都不做。 window.destory() 在这里也不做任何事情。您的按钮命令应该保存对函数的引用,而不是像这样调用函数:command = returNcommand = returN2

标签: python tkinter import


【解决方案1】:

您的代码中有 3 个主要缺陷。

  1. Login_or_create = Menu_window.start() 在此处将始终为 None。这不是从按钮上的返回命令中分配的值。

  2. 当按钮调用函数并且您尝试返回某些内容时,按钮会简单地忽略该返回值。它永远不会分配给您调用 Menu_window.Start() 的变量。

  3. 为了在类之间进行交互,您需要将一些类属性或类本身传递给另一个。所以在这种情况下,我将Menu() 传递给MenuWindow(),以便能够调用一个方法来检查按下了什么按钮。

我已编辑您的代码以更严格地遵循 PEP8 样式指南。为了便于使用,我还将您的 Menu_window 类转换为从 tkinter tk 继承。

重命名文件名以跟随类名。

起始py文件代码:

class Menu():
    def __init__(self):
        import MenuWindow
        MenuWindow.start(self)

    def check_tracker(self, tracker):
        if tracker == "Login":
            print("Logged in")

        if tracker == "Create":
            print("Creating user")

Menu()

MenuWindow.py 文件:

import tkinter as tk
from tkinter import ttk

class MenuWindow(tk.Tk):
    def __init__(self, controller):
        tk.Tk.__init__(self)
        self.title("Menu")
        self.controller = controller
        bottom_frame= tk.LabelFrame(self)
        bottom_frame.grid(row=2)
        tk.Label(text = "Hello, Please login or create an account to play!").grid(row=0)
        ttk.Button(bottom_frame, text='Login', command=self.return_n).grid(row=1)
        ttk.Button(bottom_frame, text='Create account', command=self.return_n2).grid(row=1, column=1)
        self.mainloop()

    def return_n(self):
        self.controller.check_tracker("Login")

    def return_n2(self):
        self.controller.check_tracker("Create")

def start(x):
    MenuWindow(x)

产生一个窗口,根据按下的按钮,它将从起始 py 文件代码打印到控制台:

【讨论】:

  • tk.TK.__init__(self) 行实际上是做什么的?
  • @peterwelsh 这里有一个帖子:confused about init method inside of def init,这里有很好的使用文档:Inheritance。总而言之,tk.Tk.__init__(self) 行用于初始化继承类的所有基本属性/方法。因此,因为我正在创建一个继承自 Tk 的类,所以我使用 tk.Tk.__init__(self) 以便我的自定义 Tk 类具有内置 Tk 类的所有功能。
  • 我想我明白了,但是如果这有意义的话,我将如何关闭这个界面,因为它不像被声明为一个窗口?因为通常我使用 self.window.destroy()。
  • self.destroy().
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
  • 2019-10-24
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多