我有 2 個 python 檔案FirstWindowFile和SecondWindowFile. 我在第一個按鈕中創建一個按鈕,我想在按下它后在第二個檔案中創建一個帶有按鈕的新視窗。按第二個檔案中的按鈕后,我需要更改第一個中全域變數的值。
FirstWindowFile 代碼:
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 代碼:
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)
uj5u.com熱心網友回復:
您不能從另一個檔案中的一個檔案訪問全域。這不是個好主意。你應該在使用可變明確的方式-如果你會使用list,dict或者tk.IntVar,tk.StringVar讓價值,那么你可以把它作為引數和功能,可以在改變值list,dict或者tk.IntVar,tk.StringVar你可以在其他功能這個值。
tk.IntVar也很有用,因為您可以將其分配給標簽,當您更改時,IntVar它會自動更新Label.
你只IntVar需要記住那些需要.get()并.set()有價值的作業。
主檔案
myglobal = tk.IntVar(value=0) # you have to create after `root`
Button(..., command=lambda:second_window.change_value(myglobal)
第二個視窗.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)
完整代碼:
主檔案
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
第二個視窗.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. ...
uj5u.com熱心網友回復:
我的回答與@furas' 非常相似,但說明了如何定義和使用 aclass來避免(或至少最小化)使用全域變數——無論如何,這對你想做的事情不起作用。在這種情況下,通過將其存盤在 tkinter 中IntVar并將其作為引數顯式傳遞給另一個模塊的檔案中定義的函式。
我也在很大程度上嘗試遵循他(和我)強烈推薦的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)
uj5u.com熱心網友回復:
這是我認為您遇到的問題的更一般的答案:
從 開始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 的函式
如果您需要進一步說明,請詢問!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/323741.html
