0

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'


5
  • 1
    please format your code so we can run it Commented Nov 30, 2020 at 18:34
  • The solution depends on how you manage the multiple frames. The comment in switch_frame implies you will have destroyed StartPage by the time you create Hashtag_page. If that's the case, you can't get the value. Please provide a minimal reproducible example we can actually run. Commented Nov 30, 2020 at 18:38
  • Can I store the value in global or somewhere outside StartPage I will change to can run Commented Nov 30, 2020 at 18:42
  • You should plan your GUI before coding it, if you haven't. And yes you can store a global variable Commented Nov 30, 2020 at 18:43
  • I update the code I try with global but I didn`t get a result Commented Nov 30, 2020 at 18:56

3 Answers 3

0

In the code below, i have been storing the credentials inside the master (SampleApp) which is accessible from your class PageOne aswell.

Alternatively, you may consider storing the credentials in a dict, and passing it to your PageOne during initialization.

import tkinter as tk
from tkinter import *
#from PIL import Image, ImageTk
from tkinter import filedialog


class SampleApp(tk.Tk):
    def __init__(self):
        self.test = "I am the MasterAPP"
        tk.Tk.__init__(self)
        self._frame = None
        self.title('Instagram Bot')
        self.geometry("500x500")
        self.switch_frame(StartPage)

        self.username = None
        self.password = None

    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()


        tk.Button(self, text="Login",
            command=lambda:
                self.login()
        ).pack()


    def login(self):
        self.save_credentials()
        self.master.switch_frame(PageOne)


    def save_credentials(self):
        self.master.password = self.password.get()
        self.master.username = self.username.get()




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(self.master.username, self.master.password)






if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

0

I would make a dictionary of the username and password, but depending on your GUI, I don't know the best option for you

Make the dictionary

dictionary = {}

class StartPage(tk.Frame):
   ...

Add values for the username and password.

def login_credentials(self):
    dictionary['password'] = self.password.get()
    dictionary['username'] = self.username.get()

Then you can just print them

def start_hashtag(self):
    print(dictionary['username'],dictionary['password'])

1 Comment

I get empty variable maybe its from tkinter
0

There are few issues in your code:

  • should not call self.password = self.password.get() and self.username = self.username.get() just after self.username and self.password are created because they will be both empty strings and override the references to the two Entry widgets.

  • username and password are instance variables of StartPage, so they cannot be access by StartPage.username and StartPage.password

  • two __init__() function definitions in PageOne class

  • when switching frame, the previous frame is destroyed. So username and password will be destroyed as well and cannot be accessed anymore.

You can use instance of SampleApp to store the username and password, so they can be accessed by its child frames:

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()
        tk.Button(self, text="Login", command=self.login).pack()

    def login(self):
        # store the username and password in instance of 'SampleApp'
        self.master.username = self.username.get()
        self.master.password = self.password.get()
        # switch to PageOne
        self.master.switch_frame(PageOne)

class PageOne(tk.Frame):
    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
        if filename:
            self.hastag_lbl.configure(text = filename)

    def start_hashtag(self):
        # get username and password from instance of SampleApp
        print(self.master.username, self.master.password)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.