verschiedene Klassen auf dieselben Variablen zugreifen lassen
Verfasst: Mittwoch 25. November 2020, 21:49
Hi
bin noch sehr neu in Python und komme nicht weiter..
ich habe diese kleine app, welche zwischen frames hin und herwechseln kann:
soweit so gut.. jetzt habe ich da aber noch weitere Klassen für weitere Frames und ich möchte dass alle eine gemeinsame daten-"Basis" nutzen .. so wie in C+ als wenn ich mir eine static class anlege und und einfach mit myStaticClass.myVar darauf zugreife.. z.b. sollen bei einem erfolgreichen login, die user-daten in mehreren variablen gespeichert werden, welche wiederrum überall zugänglich sind (lesen/schreiben)
das erste print() gibt mir "test" aus.. logisch.. das zweite gibt mir den wert den es zugewiesen bekommen hat, aus... soweit klar.. aber nachdem ich später in einer anderen klasse print(user.user_name) mache, gibt er mir wieder "test" aus (?)
da gibts doch bestimmt eine adequate lösung für(?). könnte mich freundlicherweise jemand in die richtie richtung schubsen?
bin noch sehr neu in Python und komme nicht weiter..
ich habe diese kleine app, welche zwischen frames hin und herwechseln kann:
Code: Alles auswählen
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold")
self.txt_font = tkfont.Font(family='Helvetica', size=12)
self.title("MyTest2")
self.geometry('600x300')
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
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 (Page_logIn, Page_Content, PageTwo):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("Page_logIn")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
class Page_logIn(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
# - header title
canvas_width = 600
canvas_height = 100
canvas = tk.Canvas(self, width = canvas_width, height = canvas_height, bd=0, highlightthickness=0, relief='ridge')
canvas.pack()
self.img = tk.PhotoImage(file = "images/bg.png")
canvas.create_image(-20, 0, anchor = "nw", image = self.img)
canvas.create_text(canvas_width / 2 , canvas_height / 2, font=("Helvetica", 34, 'bold'), text="Portal V2", fill="#FFFFFF")
# - title
label = tk.Label(self, text = "Willkommen", font = controller.txt_font)
label.pack(side="top", pady=10)
# - login form
labelframeLogin = tk.LabelFrame(self, text = "Bitte authentifizeren Sie Ihren Zugriff")
labelframeLogin.pack(expand="yes")
labelUser = tk.Label(labelframeLogin, text='Benutzer:')
labelUser.pack(padx=10, pady=10, side='left', fill='y')
self.controller.entryUser = tk.Entry(labelframeLogin)
self.controller.entryUser.pack(padx=10, pady=10, side='left', fill='y')
labelPwd = tk.Label(labelframeLogin, text='Passwort:')
labelPwd.pack(padx=10, pady=10, side='left', fill='y')
self.controller.entryPwd = tk.Entry(labelframeLogin)
self.controller.entryPwd.pack(padx=10, pady=10, side='left', fill='y')
# - login button
button1 = tk.Button(self, text = "Einloggen", width = 20, command = lambda: controller.doLogin(self.controller.entryUser.get(), self.controller.entryPwd.get()))
button1.pack(padx=10, pady=10)
self.controller.labelLoginMsg = tk.Label(self, text="", fg="#FF0000")
self.controller.labelLoginMsg.pack(padx=10, pady=10, fill="x")
jetzt mache ich irgendwo im code sowas hierclass user:
user_name = "test"
user_fname = ""
user_userid = ""
user_customerid = ""
user_groupid = ""
Code: Alles auswählen
print(user.user_name)
user.user_name = x["USER_DESC"]
user.user_fname = x["LOGIN"]
user.user_userid = x["USER_ID"]
user.user_customerid = x["CUSTOMER_ID"]
user.user_groupid = x["GROUP_ID"]
print(user.user_name)
da gibts doch bestimmt eine adequate lösung für(?). könnte mich freundlicherweise jemand in die richtie richtung schubsen?
