【问题标题】:Variable isn't declaring inside of my function?变量没有在我的函数内部声明?
【发布时间】:2020-10-17 13:31:28
【问题描述】:

我在函数'lib_window' 内创建了变量'game_window',但控制台一直说未使用局部变量'game_window' 的值。我不知道那是什么意思。我的目标是简单地将新窗口与'start_Btn' 链接起来。我什至有'command=lib_window()' 在其中。

import tkinter as tk

class windowLib:

    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry('700x500')
        self.root.title('UnLibs')
        self.root['background'] = '#91a8e3'
        self.start_Btn = tk.Button(self.root,
                                   command=lib_window(),
                                   text="PLAY",
                                   foreground="white",
                                   highlightbackground="red",
                                   font=("Helvetica", 40))
        self.welcome_Label = tk.Label(self.root,
                                      text="Welcome to UnLibs",
                                      font=("Helvetica", 50),
                                      bg="#91a8e3",
                                      fg="white")
        self.start_Btn.pack(side="bottom", padx=10, pady=50)
        self.welcome_Label.pack(side="top", padx=0, pady=150)
        self.root.mainloop()

def lib_window():
   
    game_window = tk.Toplevel(windowLib)

windowLib()

【问题讨论】:

    标签: python button tkinter


    【解决方案1】:

    我认为你在寻找什么:

    import tkinter as tk
    from tkinter import Toplevel
    
    
    def lib_window():
    game_window = tk.Toplevel (windowLib)
    
    windowLib ()
    
    
    class windowLib:
    
    def __init__(self):
        self.root = tk.Tk ()
        self.root.geometry ('700x500')
        self.root.title ('UnLibs')
        self.root['background'] = '#91a8e3'
        self.start_Btn = tk.Button (self.root,
                                    command=lib_window (),
                                    text="PLAY",
                                    foreground="white",
                                    highlightbackground="red",
                                    font=("Helvetica", 40))
        self.welcome_Label = tk.Label (self.root,
                                       text="Welcome to UnLibs",
                                       font=("Helvetica", 50),
                                       bg="#91a8e3",
                                       fg="white")
        self.start_Btn.pack (side="bottom", padx=10, pady=50)
        self.welcome_Label.pack (side="top", padx=0, pady=150)
        self.root.mainloop ()
    

    【讨论】:

      【解决方案2】:
      import tkinter as tk
      
      class windowLib:
      
          def __init__(self):
              self.root = tk.Tk()
              self.root.geometry('700x500')
              self.root.title('UnLibs')
              self.root['background'] = '#91a8e3'
              self.start_Btn = tk.Button(self.root,
                                         command=lib_window(),
                                         text="PLAY",
                                         foreground="white",
                                         highlightbackground="red",
                                         font=("Helvetica", 40))
              self.welcome_Label = tk.Label(self.root,
                                            text="Welcome to UnLibs",
                                            font=("Helvetica", 50),
                                            bg="#91a8e3",
                                            fg="white")
              self.start_Btn.pack(side="bottom", padx=10, pady=50)
              self.welcome_Label.pack(side="top", padx=0, pady=150)
              self.root.mainloop()
      
              game_window = tk.Toplevel(windowLib)
      if __name__ == '__main__':
          windowLib()
      

      这可能有效....

      【讨论】:

      • 恕我直言,最好添加一个关于错误和原因的解释。
      猜你喜欢
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 2015-02-10
      • 2021-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多