The Dude
The Dude

Reputation: 59

Python and PySide: Variable values reset when calling functions from another file

I've been writing this python script which basically sets your windows wallpaper background through reddit links and specific variables you set, like links from hot/top all time/etc. and i'm writing a GUI with PySide for it, currently i have 3 files.

When i run the output.py file, set the values inside the GUI everything works correctly, the values are set fine, but when i click the start button and the script executes (WallDit.py) and calls the functions from output.py the variable values get reset to the default values (i.e. i set to "Wallpapers", it gets set back to the default "Wallpaper+Wallpapers").

For instance when i call this function from the 1st file, the subreddit line text value gets set back to default, no the one that is actually displayed on the GUI:

def handle_subreddit_line(self):
    print("subreddit line in output.py: " + self.subreddit_line.text())
    return self.subreddit_line.text()

Calling the function code:

def get_subreddit_name():
    test = MainWindow()
    print("test: " + test.handle_subreddit_line())
    subreddit = MainWindow().handle_subreddit_line()
    return subreddit

Note: When i change the values, say of the subreddit_name line the signal gets sent and the correct value outputs, the issue starts when the function gets called from the 1st file.

Update: If i call the function anywhere, the value still gets reset to the default

def handle_start_btn(self):
    if self.start_btn.isDown:
        print("Button pressed")
    print("inside function: " + MainWindow().handle_subreddit_line())
    import WallDit

Upvotes: 1

Views: 227

Answers (1)

Chris Warrick
Chris Warrick

Reputation: 1619

In WallDit.py, you initialize FIVE new windows. Which means you have five new text boxes. In order to fix your problem, you need to use a single instance, the one you create in output.py. For example, you can delete main() in WallDit.py, make set_wallpaper and other functions take a window argument, and do something like this:

def handle_start_btn(self):
    WallDit.set_wallpaper(self)

(move the import line to the top of the file, since WallDit will not execute anything on import now and it’s good practice to keep all imports there)

edit: of course, you need to pass the window around and use it instead of MainWindow().

Upvotes: 1

Related Questions