Here is the full code for test please run it and tell me if I can store username and password outside the class after destroy the frame
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.title('Instagram Bot')
self.geometry("500x500")
self.switch_frame(StartPage)
def switch_frame(self, frame_class):
"""Destroys current frame and replaces it with a new one."""
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
class StartPage(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
tk.Label(self, text="Enter Username").pack(side="top", fill="x", pady=10)
self.username = tk.Entry(self,)
self.username.pack()
tk.Label(self, text='Enter Pssword').pack(side='top', fill='x', pady=10)
self.password = tk.Entry(self)
self.password.pack()
self.password = self.password.get()
self.username = self.username.get()
# if(username.get('text') == None or password.get('text') == None):
# tk.Button(self, text="Login",
# command=lambda: master.switch_frame(StartPage)).pack()
# else:
tk.Button(self, text="Login", command=lambda: master.switch_frame(PageOne) and login_data()).pack()
class PageOne(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
tk.Label(self, text="Welcome").pack(side="top", fill="x", pady=10)
tk.Button(self, text='Some text', command=lambda: master.switch_frame(Hashtag_page)).pack()
def __init__(self, master):
tk.Frame.__init__(self, master)
self.hastag_lbl = tk.Label(self, text='Choice file')
self.hastag_lbl.pack(side='top',fill='x',pady=10)
tk.Button(self, text='browse', command=self.browseFiles_hstg).pack()
tk.Button(self, text='Start Bot', command=self.start_hashtag).pack()
def browseFiles_hstg(self):
filename = filedialog.askopenfilename(initialdir = "/",
title = "Select a File",
filetypes = (("Text files",
"*.txt*"),
("all files",
"*.*")))
# Change label contents
self.hastag_lbl.configure(text = filename)
def start_hashtag(self):
print(StartPage.username, StartPage.password)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python3.6/tkinter/init.py", line 1705, in call return self.func(*args) File "scofrlw.py", line 71, in start_hashtag print(StartPage.username, StartPage.password) AttributeError: type object 'StartPage' has no attribute 'username'
switch_frameimplies you will have destroyedStartPageby the time you createHashtag_page. If that's the case, you can't get the value. Please provide a minimal reproducible example we can actually run.