mikegrep
mikegrep

Reputation: 27

I try to use tk enrty text from one class to another in python, tkinter

I am trying to user username1 and username 2 tk entry text from class StartPage to class Hashtag page when I test with print in HastaPage I get result :

<class 'tkinter.Entry'> .!hashtag_page.!startpage2.!entry2

Also I am trying to use a function in def set_time() from StartPage to HastagPage class start_hastag() func. Output is

NameError: name 'set_time' is not defined

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()
         global password1, username1
         password1 = self.password
         username1 = self.username
         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)).pack()
     def set_time():
         time.sleep(160)

class Hashtag_page(tk.Frame):
    def  __init__(self, master):
        tk.Frame.__init__(self, master)
        self.hastag_lbl = tk.Label(self, text='Choice file with hashtags')
        self.hastag_lbl.pack(side='top',fill='x',pady=10)
        tk.Button(self, text='Browse files', command=self.browseFiles_hstg).pack()
        tk.Label(self, text='Enter how much likes, follows action per hastag').pack()
        self.amount_num = tk.Entry(self).pack()
        tk.Button(self, text='Start Bot', command=self.start_hashtag).pack()
        tk.Button(self, text='Back', command=lambda: master.switch_frame(PageOne)).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):
        username_ = StartPage(self).username1
        password = StartPage(self).password1
        print(username1.get(),password1.get())
        set_time()

Upvotes: 0

Views: 41

Answers (1)

broderick
broderick

Reputation: 77

it looks like you are trying to call a function nested within a class. This is allowed, but make sure to call set_time() with StartPage.set_time()

My advice would be to move the set_time() method out of the StartPage class and put it in the Hashtag_page class entirely, as it's never called in StartPage, but this is up to you.

Check out this example of calling ExampleClassA's method inside ExampleClassB

class ExampleClassA:
    def add_two_in_a(num1, num2):
        return num1 + num2

class ExampleClassB:
    def __init__(self):
        print("Initialized a new 'B' class")
        result1 = ExampleClassA.add_two_in_a(1, 3)
        result2 = ExampleClassA.add_two_in_a(2, 5)
        print("Calling a function in class Example class A from example class B")
        print("Result 1 is {}\nResult 2 is {}".format(result1, result2))

if __name__ == "__main__":
    ExampleClassB()

Upvotes: 1

Related Questions