我正在使用a solution發現可以在我的代碼中共享變量,其中包含'Frame'類。然而,我試圖改變這些共享變量的值的任何嘗試似乎都沒有效果,並且在我嘗試更改它們後,如果我打印它只是返回空白。任何幫助,將不勝感激。Tkinter更改共享變量
class GolfApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.shared_data = {
"currentcourse": tk.StringVar(),
"numberofteams": tk.IntVar()}
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (MainMenu, CreatePage, ViewPage, GetTeamsPage, ChooseCourse,
AddCourse, LoginSignUp, Login, SignUp, Highscores1, Highscores2,
Scorecards1, Scorecards2):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("LoginSignUp")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
這是可以找到鏈接的解決方案的地方。我有兩個變量,'currentcourse'和'numberofteams',我需要從一個框架分享給其他人。我試圖在以下代碼位中的兩個不同類中設置這些變量。
class GetTeamsPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.configure(background="lightgreen")
def set_teamnumber():
numberofteams = answerentry.get()
numberofteams = self.controller.shared_data["numberofteams"].get()
def testInt(inStr, i, acttyp):
ind = int(i)
if acttyp == '1':
if not inStr[ind].isdigit():
return False
return True
for col in range(7):
self.grid_columnconfigure(col)
for row in range(5):
self.grid_rowconfigure(row)
questionlbl = tk.Label(self,
text="How many teams/players are there?",
bg="lightgreen",
font = "Verdana 20 bold")
questionlbl.grid(column=2,
row=0,
columnspan=3)
answerentry = tk.Entry(self,
text="Enter a number here.",
validate = "key",
textvariable=self.controller.shared_data["numberofteams"])
answerentry.grid(column=2,
row=2,
columnspan=3)
moveonbtn = tk.Button(self,
text="Continue",
height = "3",
width = "40",
bg="darkgreen",
fg="lightgreen",
command = lambda: (controller.show_frame("CreatePage"), set_teamnumber()))
moveonbtn.grid(column=1,
row=5,
columnspan=3)
returnbtn = tk.Button(self,
height="3",
width="40",
bg="darkgreen",
fg="lightgreen",
text="Return to main menu",
command = lambda: controller.show_frame("MainMenu"))
returnbtn.grid(column=4,
row=5,
columnspan=3)
class ChooseCourse(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.configure(background="lightgreen")
cursor.execute("SELECT CourseName FROM Course")
coursetuple = cursor.fetchall()
courselist = [row[0] for row in coursetuple]
def get_choice():
currentcourse = self.controller.shared_data["currentcourse"]
currentcourse = listmenu.get()
for col in range(2):
self.grid_columnconfigure(col, minsize=50)
for row in range(7):
self.grid_rowconfigure(row, minsize=60)
titlelbl = tk.Label(self,
text="Choose a course",
bg="lightgreen",
font = "Verdana 20 bold")
titlelbl.grid(column=2,
row=0)
addbtn = tk.Button(self,
text="Add a new course",
bg="darkgreen",
fg="lightgreen",
command = lambda: controller.show_frame("AddCourse"))
addbtn.grid(column=2,
row=3)
continuebtn = tk.Button(self,
text="Continue",
bg="darkgreen",
fg="lightgreen",
command = lambda: (controller.show_frame("GetTeamsPage"), get_choice))
continuebtn.grid(column=2,
row=4)
returnbtn = tk.Button(self,
text="Return to main menu",
bg="darkgreen",
fg="lightgreen",
command = lambda: controller.show_frame("MainMenu"))
returnbtn.grid(column=2,
row=5)
listmenu = tk.Listbox(self)
for x in range(0, len(courselist)):
listmenu.insert("end", courselist[x])
listmenu.grid(column=2,
row=1)
然後將值設置自動;你不需要set_teamnumber方法。我不想進一步涉及你的代碼;如果您需要更多幫助,請提供[最小,完整和可驗證示例](https://stackoverflow.com/help/mcve)。 – Novel