我遇到了一個變數問題。無法理解錯誤。我的目標是使用以下代碼從 Radiobutton 獲得價值。
import tkinter as tk
class App:
def __init__(self, root):
root.title("undefined")
width=600
height=500
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d %d %d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
R1 = tk.Radiobutton(root, text="Option 1", variable=var, value='10',
command=sel)
R1.place(x=100,y=70,width=85,height=25)
R2 = Radiobutton(root, text="Option 2", variable=var, value='12',
command=sel)
R2.place(x=200,y=70,width=85,height=25)
def sel():
selection = "You selected " str(var.get())
print(selection)
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
uj5u.com熱心網友回復:
您尚未在var任何地方定義變數,因此您無法將其傳遞給您的 Radiobutton。您需要先宣告變數,然后才能使用它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/477340.html
