简体   繁体   English

无法在tkinter python中将一个函数的变量用于另一个函数

[英]unable to use variable of one function to another function in tkinter python

I am trying to create the GUI, I am unable to use the value of a variable in one function on the other function in tkinter 我正在尝试创建GUI,无法在tkinter的另一个函数上的一个函数中使用变量的值

But, in normal functions, when we declare the variable as global, it is accessible. 但是,在普通函数中,当我们将变量声明为全局变量时,它是可访问的。

Here is my code, 这是我的代码,

from tkinter import *
global chosen_option
root = Tk()
root.title("Drop-down boxes for option selections.")

var = StringVar(root)
var.set("drop down menu button")

def grab_and_assign(event):
    grab_and_assign.var = var.get()
    label_chosen_variable= Label(root, text=chosen_option)
    label_chosen_variable.grid(row=1, column=2)
    print(chosen_option)

def pri():
    print(grab_and_assign.chosen_option)

drop_menu = OptionMenu(root, var,  "one", "two", "three", "four", "meerkat", "12345", "6789", command=grab_and_assign)
drop_menu.grid(row=0, column=0)

label_left=Label(root, text="chosen variable= ")
label_left.grid(row=1, column=0)
pri()

root.mainloop()

You have two problems: 您有两个问题:

First: mainloop() starts window so your pri() is executed before you see window - so before you could selecte anything. 首先: mainloop()启动window,以便在看到window之前先执行pri() -在选择任何内容之前。 I add pri to button so you can also see how it works after selecting option. 我在按钮上添加了pri ,因此选择选项后您还可以查看其工作方式。

Second: all variables create outside functions/classes are global and they don't need keyword global . 其次:所有创建外部函数/类的变量都是全局变量,它们不需要关键字global You have to use global inside function to inform function that when you will assign value using = then you what to use external/global variable instead of creating local one. 您必须使用global内部函数来通知函数,何时使用=分配值,那么您将使用外部/全局变量而不是创建局部变量。

In code I have global variable normal_var and I use global normal_var inside grab_and_assign to inform function to assign value to global variable and not to create local variable in line 在代码中,我具有全局变量normal_var并且在grab_and_assign使用global normal_var来通知函数将值分配给全局变量,而不是在行中创建局部变量

 normal_var = var.get()

I don't have to use global inside pri() because I only get value from variable nromal_var and don't use = to assign new value 我不必在pri()使用global因为我仅从变量nromal_var获取值,而不必使用=来分配新值

I don't have to use global to assign new value for: 我不必使用global为以下项分配新值:

  • var because it uses set() instead of = . var因为它使用set()而不是=
  • label_selected["text"] because it assign value to key in dictionary. label_selected["text"]因为它为字典中的键分配值。

But I would have to use global for label_selected if I would like to assign new Label inside function - ie. 但是,如果我想在函数内部分配新的Label ,即必须为label_selected使用global label_selected = tk.Label(..)

import tkinter as tk

# --- functions ---

def grab_and_assign(event):
    global normal_var

    normal_var = var.get()

    label_selected["text"] = var.get()

def pri():
    print('normal_var:', normal_var)
    print('var.get():', var.get())
    print('---')

# --- main ---

# global variable
normal_var = "- empty -"

root = tk.Tk()
root.title("Drop-down boxes for option selections.")

# it is global variable
var = tk.StringVar(root)
var.set("drop down menu button")

drop_menu = tk.OptionMenu(root, var,  "one", "two", "three", "four", "meerkat", "12345", "6789", command=grab_and_assign)
drop_menu.grid(row=0, column=0)

label_left = tk.Label(root, text="chosen variable = ")
label_left.grid(row=1, column=0)

# create it only once
label_selected = tk.Label(root, text="?")
label_selected.grid(row=1, column=1)

# execute `pri` after button press
b = tk.Button(root, text='PRI', command=pri)
b.grid(row=2, column=0)

# execute `pri` before window starts
pri()

root.mainloop()

BTW: Label has textvariable= and you can assign var which you use in OptionMenu and then it will automatically display selected value 顺便说一句: Label具有textvariable=和可以分配var您在使用OptionMenu ,然后它会自动显示选择的值

import tkinter as tk

# --- main ---

root = tk.Tk()

var = tk.StringVar(root)
var.set("drop down menu button")

label_selected = tk.Label(root, textvariable=var)
label_selected.pack()

drop_menu = tk.OptionMenu(root, var,  "one", "two", "three", "four", "meerkat", "12345", "6789")
drop_menu.pack()

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM