[英]How do I carry over a variable from one class to another?
I'm trying to program a application that carries over user inputs from one page to the other where the pages are separated by classes.我正在尝试编写一个应用程序,该应用程序将用户输入从一个页面传送到另一个页面,其中页面由类分隔。 A problem I'm having is that the array output on page 2 isn't updating.
我遇到的一个问题是第 2 页上的数组输出没有更新。 Basically the output is just [] which is just the starting initialized variable.
基本上输出只是 [] ,它只是开始的初始化变量。 I've tried some solutions I've found on stack overflow such as calling it a global variable again at the init bit of the PageTwo class as well as trying to use PageOne.overall_data and using self.overall_data but the problem persists although there weren't any errors.
我尝试了一些在堆栈溢出时找到的解决方案,例如在 PageTwo 类的 init 位再次调用它为全局变量,以及尝试使用 PageOne.overall_data 和使用 self.overall_data 但问题仍然存在,尽管没有没有任何错误。 This is a part of the code before I tried anything.
这是我尝试任何事情之前代码的一部分。 I tried to cut out the irrelevant bits but I'm not sure if I cut out too much, feedback is much appreciated.
我试图剪掉不相关的部分,但我不确定我是否剪掉了太多,非常感谢反馈。 Thanks for reading!
谢谢阅读!
import tkinter as tk
import tkinter_nav as tknav
from tkinter import ttk
import numpy as np
class App(tknav.Wrapper):
def __init__(self):
tknav.Wrapper.__init__(
self,
pages=[PageOne, PageTwo],
start_state={'previous_page': None}
)
self.geometry('450x450')
self.show_page('page_one')
class PageOne(tknav.Page):
def __init__(self, parent):
tknav.Page.__init__(self, parent, 'page_one')
player_details = []
global overall_data
overall_data = []
my_notebook = ttk.Notebook(self)
my_notebook.pack(pady = 15)
my_notebook.pack(fill="both", expand=True)
my_frame1 = tk.Frame(my_notebook, width = "500", height = "500")
def submit(): #called every time inputs are made to be appended to player_details then overall_data
player_details = []
global overall_data
player_details.append(name.get())
player_details.append(health.get())
player_details.append(ac.get())
player_details.append(initiative.get())
overall_data.append(player_details)
overall_data = sorted(overall_data, key = lambda x:x[3])
print(str(overall_data))
class PageTwo(tknav.Page): #second page of the application
def __init__(self, parent):
tknav.Page.__init__(self, parent, 'page_two')
tk.Label(
self,
text='Page Two'
).pack()
tk.Button(
self,
text='Navigate to Page One',
command=lambda: self.__navigate(),
).pack()
line21 = tk.Label(self, text = str(overall_data), font = ('Times New Roman', 12))
line21.place(x = 30, y = 30, width = 100, height = 25)
def __navigate(self):
print('navigating to page one')
self.navigate('page_one')
if __name__ == '__main__':
App().mainloop()
You can put your data at the class level in PageOne
like this:您可以将数据放在
PageOne
中的类级别,如下所示:
class PageOne(tknav.Page):
overall_data = []
and use it everywhere like this:并像这样在任何地方使用它:
PageOne.overall_data.append(player_details)
Since you have used tkinter_nav
, you can use its provided app_state
to share data between pages:由于您使用了
tkinter_nav
,您可以使用其提供的app_state
在页面之间共享数据:
class App(tknav.Wrapper):
def __init__(self):
tknav.Wrapper.__init__(
self,
pages=[PageOne, PageTwo],
start_state={'previous_page': None, 'overall_data': []} # added 'overall_data'
)
self.geometry('450x450')
self.show_page('page_one')
Then you can access this shared data in each page by:然后您可以通过以下方式访问每个页面中的共享数据:
self.app_state['overall_data']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.