user11838224
user11838224

Reputation:

How can I change the Button without the code opening a new window with the changed button

Im trying to make a simple shop which changes the background of the main window. But when you click the buy button (buyblue) it should change the text on the button to gekauft(sold in german). But because of the button beeing in another definition i acessed it with return and when i now click the button it opens a new window with the changed button. Is there a way to fix it that only the button on the already open window changes and no new window opens?

Its written in python 3 and with the tkinter module.

click = 10
bluecost = 10
def openshop():
    global root2
    root2 = Tk()
    root2.title("SIMPLE CLICKER SHOP")
    root2.geometry("700x400")
    root2.resizable(height = False, width = False)
    root2.configure(bg = "white")


    buttonblue = Button(root2, text = "3000 Clicks", bg = "maroon1", command = buyblue)
    buttonblue.place(x = 120, y = 120)
    return buttonblue

def buyblue():
    global click
    global bluecost
    buttonblue=openshop()

    if click >= int(bluecost):
            click -= int(bluecost)
            global root
            global label
            global click1
            root["bg"]="blue"
            click1["bg"]="blue"
            label["bg"]="blue"
            buttonblue["bg"]="green"
            buttonblue["text"] = "Gekauft"
            bluecost = 0

Is there another way to pass a variable which is used in a defintion to another defintion?

Upvotes: 0

Views: 37

Answers (1)

Nouman
Nouman

Reputation: 7303

You are creating the tkinter window in the function openshop. It means that whenever you run the function openshop, another window will be created. You have to modify your function from:

...
def openshop():
    global root2
    root2 = Tk()  #< creates the window
    ...

to:

...
global root2
root2 = Tk() #< creates the window
def openshop():
    ...

This will reconfigure the root each time the openshop is run again but will not create another window.

Upvotes: 1

Related Questions