【问题标题】:Setting global variables between classes with tKinter使用 tKinter 在类之间设置全局变量
【发布时间】:2016-10-01 07:04:20
【问题描述】:

我目前正在努力使用从另一个类(使用 tkinter)派生的输入来更改不同类中的输出。这是我的代码:

#there are many of these defs, this is just one i used as an example

    def command_ssumowrestler(self):
        self.master.withdraw()
        toplevel = tk.Toplevel(self.master)
        toplevel.geometry("30x70")
        app=ClassConfirmation(toplevel)
        global cs_hobby
        cs_hobby = 'sumo_wrestler'

class ClassConfirmation:
    def __init__ (self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        confirmclass_label = tk.Label(master, width = 20, fg = 'blue',text= "Do You Wish to Proceed as:")
        confirmclass_label.pack()

#here i use the variable defined and set globally in the previously block, which always leaves me with an error saying cs_hobby is not defined 

        classdescription_label = tk.Label(master, width =50, text='test ' + cs_hobby + ' hello')
        classdescription_label.pack()
        self.confirmclassButton = tk.Button(self.frame, text = 'Confirm', width = 20, command = self.close_windows)
        self.confirmclassButton.pack()
        self.frame.pack()
    def close_windows (self):
        self.master.withdraw()
        app=dragonrealmP1
#this just opens a different class

【问题讨论】:

    标签: python class variables tkinter


    【解决方案1】:

    global 不创建变量——它只通知函数/方法使用全局变量而不是局部变量。首先你必须在类之前创建这个变量。


    顺便说一句:如果你使用类,那你为什么不在头等舱使用呢

    self.cs_hobby = 'sumo_wrestler'
    

    ClassConfirmation

    sone_object_name.cs_hobby 
    

    app = ClassConfirmation(toplevel, 'sumo_wrestler')
    
    
    class ClassConfirmation:
        def __init__ (self, master, cs_hobby):
    
            # here use cs_hobby 
    

    编辑:

    首先:global cs_hobby 不创建全局变量。它只通知函数使用全局cs_hobby 而不是本地cs_hobby

    要与global 合作,您需要

    cs_hobby = ""   # create global variable (outside classes)
    
    
    def command_ssumowrestler(self):
        global cs_hobby  # inform function to use global variable
    
        # rest 
    
        cs_hobby = 'sumo_wrestler' # set value in global variable
    
        app = ClassConfirmation(toplevel)
    
    
    class ClassConfirmation:
        def __init__ (self, master):
            global cs_hobby    # inform function to use global variable
    
            # rest 
    
            print(cs_hobby)    # get value from global variable
            cs_hobby = 'tenis' # set value in global variable
    

    无需全局变量即可工作

    def command_ssumowrestler(self):
         self.cs_hobby = 'sumo_wrestler' # create local variable
    
         app = ClassConfirmation(toplevel, self.cs_hobby) # send local value to another class
    
         self.cs_hobby = app.cs_hobby # receive value from another class
    
    
    class ClassConfirmation:
        def __init__ (self, master, hobby):
            self.cs_hobby = hobby # create local variable and assign value 
    
            # rest 
    
            print(self.cs_hobby)     # use local value
            self.cs_hobby = 'tenis'  # use local value
    

    【讨论】:

    • 我不明白这是什么意思以及它是如何工作的,你能解释一下吗?
    • 查看答案中的新文本
    • 所以当你把单词放在def后面的括号中时,会从类/def之外导入变量吗?
    • 当你有def fun(arg1, arg2) 并使用fun(123, "Hello") 然后fun 创建局部变量arg1arg2 并分配arg1=123arg2="Hello"。这并不意味着fun 导入外部变量arg1arg2。当您使用 fun(global_variable_1, global_variable_2) 时,fun 再次创建本地 arg1 arg2 并分配 arg1=global_variable_1, arg2=global_variable_2`。
    猜你喜欢
    • 2021-12-29
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    相关资源
    最近更新 更多