我有2个python文件,FirstWindowFile
和SecondWindowFile
。我创建了一个按钮在第1和我想,按下它后,创建第二个文件中有一个带有按钮的新窗口。在按下第二个文件中的按钮后,我需要更改1 .
全局变量的值。FirstWindowFile
code:
import tkinter as tk
from tkinter import*
import SecondWindowFile
root = Tk() # create a main window
root.geometry("750x250")
global myglobal # this is the global i want to change
myglobal = 0
btn1 = tk.Frame(root, borderwidth=5, relief="ridge")
btn1.grid(column=0, row=0)
# when I press this button I send the SecondWindowFile to ChangeValue()
Analyze = tk.Button(btn1, text="Analyze",
command=lambda: SecondWindowFile.ChangeValue()).grid(row=0, column=0)
# myglobal has to take new value (sent from SecondWindowFile) so to
# be used for new calculations
print(myglobal)
root.mainloop()
SecondWindowFile
code:
import tkinter as tk
from tkinter import*
def changeMyNum():
gl=1
# I need this value of gl to be returned to the FirstWindowFile and be the
# new value of global myglobal
def ChangeValue():
secondWindow = Tk() # create a 2nd window
secondWindow.geometry("150x150")
btn2 = tk.Frame(secondWindow, borderwidth=5, relief="ridge") # create a button
btn2.grid(column=0, row=0)
# by pressing the button goes to changeMyNum()
ChangeVal = tk.Button(secondWindow, text="Press to Change Global",
command=lambda: changeMyNum).grid(row=0, column=0)
不能从一个文件访问另一个文件中的global。这不是一个好主意。你应该以显式的方式使用变量-如果你要使用list
,dict
或tk.IntVar
,tk.StringVar
来保持值,那么你可以将它作为参数发送,函数可以在list
,dict
或tk.IntVar
,tk.StringVar
中更改值,你可以在其他函数中使用该值。
tk.IntVar
也可以很有用,因为你可以将它分配给标签,当你改变IntVar
时,它会自动更新Label
的值。
你只需要记住IntVar
需要.get()
和.set()
来处理value。
main.py
myglobal = tk.IntVar(value=0) # you have to create after `root`
Button(..., command=lambda:second_window.change_value(myglobal)
second_window.py
def change_value(variable):
Button(..., command=lambda:change_my_num(variable))
def change_my_num(variable):
variable.set( variable.get()+1 )
print(variable.get())
现在你可以把它传递给Label
来显示当前值
Label(..., textvariable=myglobal)
完整代码:
main.py
import tkinter as tk
#from tkinter import * # PEP8: `import *` is not prefered
import second_window
#
root = tk.Tk() # create a main window
root.geometry("750x250")
myglobal = tk.IntVar(value=0) # you have to create after `root`
btn1 = tk.Frame(root)
btn1.grid(column=0, row=0)
# PEP8: `lower_case_names` for variables
analyze = tk.Button(btn1, text="Analyze", command=lambda:second_window.change_value(myglobal))
analyze.grid(row=0, column=0)
l = tk.Label(btn1, textvariable=myglobal)
l.grid(row=1, column=0)
print(myglobal.get()) # it runs it before `mainloop` starts GUI and shows window - so it is useless.
root.mainloop()
print(myglobal.get()) # it runs it after closing window
second_window.py
import tkinter as tk # PEP8: `import *` is not preferred
def change_my_num(variable): # PEP8: `lower_case_names` for functions
variable.set( variable.get()+1 )
print(variable.get())
def change_value(variable):
second_window = tk.Toplevel() # use `Toplevel to create a 2nd window
b = tk.Button(second_window, text="Press to Change Global", command=lambda:change_my_num(variable))
b.grid(row=0, column=0)
PEP 8——Python代码风格指南
PEP 20—Python Zen
... Explicit is better than implicit. ...
我的答案与@furas'非常相似,但说明了如何定义和使用class
来避免(或至少最小化)使用全局变量-这对您想要做的事情不起作用。在本例中,通过将其存储在tkinterIntVar
中,并显式地将作为参数传递给另一个模块文件中定义的函数。
我也在很大程度上尝试遵循他(和我)强烈推荐的PEP 8 - Python代码风格指南,特别是关于命名约定部分(也适用于模块文件名称)。
first_window.py
import tkinter as tk
import second_window as sw
class MyApp:
def __init__(self, master):
self.frame = tk.Frame(master, borderwidth=5, relief="ridge")
self.frame.grid(row=0, column=0)
self.my_var = tk.IntVar(master=master, value=0)
analyze_btn = tk.Button(self.frame, text="Analyze",
command=lambda: sw.change_value(self.my_var))
analyze_btn.grid(row=0, column=0, columnspan=2)
tk.Label(self.frame, text="My var:").grid(row=1, column=0)
tk.Label(self.frame, textvariable=self.my_var).grid(row=1, column=1)
if __name__ == '__main__':
root = tk.Tk() # Create a main window.
root.geometry("750x250")
my_app = MyApp(root)
root.mainloop()
second_window.py
import tkinter as tk
def change_my_num(var):
var.set(var.get() + 1) # Increment value in IntVar.
def change_value(var):
second_window = tk.Tk() # Create a 2nd window.
second_window.geometry("150x150")
frame2 = tk.Frame(second_window, borderwidth=5, relief="ridge")
frame2.grid(row=0, column=0)
change_val_btn = tk.Button(frame2, text="Press to Change Global",
command=lambda: change_my_num(var))
change_val_btn.grid(row=0, column=0)
done_btn = tk.Button(frame2, text="Done", command=second_window.destroy)
done_btn.grid(row=1, column=0)
我认为你的问题有一个更一般的答案:
从SecondWindowFile
开始,定义一个函数,该函数创建一个GUI,该GUI在按下按钮时自行结束并返回一个变量。
在FirstWindowFile
中,将变量定义为SecondWindowFile
函数的返回值。
,
FirstWindowFile:
global myVar
def onButtonPress():
return SecondWindowFile.getValue()
myVar = onButtonPress()
SecondWindowFile:
def getValue():
def onButtonPress():
return 'The Value You Want'
getValue()
是您创建的返回var
的函数如果您需要进一步的说明,请询问!