0

My code is not working out for some reason. But when I don't open my file in a function it will work. So I need help opening a python file in my menu file. Here is the code:

first file:

from tkinter import *

def Game():

    mult = 1
    clicks = 0
    clickgame = Tk()
    clickgame.geometry("500x500")

    clickgame.title("Techno Clicker")
        
    def buttonCommand():
        global clicks
        clicks += 1*(mult)  
        clickslabel.config(text=clicks)

    def multiplyerX1():
        global clicks
        global mult
        if clicks > 99:
            clicks -= 100
            mult += 1
            clickslabel.config(text=clicks)
        else:
            lickslabel.config(text="Not Enough clicks")

             
    def multiplyerX5():
        global clicks
        global mult
        if clicks > 499: 
            clicks -= 500
            mult += 5
            clickslabel.config(text=clicks)
        else:
            clickslabel.config(text="Not Enough clicks")

    def multiplyerX10():
        global clicks
        global mult
        if clicks > 999:
            clicks -= 1000
            mult += 10
            clickslabel.config(text=clicks)
        else:
            clickslabel.config(text="Not Enough clicks")

    Background_btn = PhotoImage(file="/Users/PrivateInfo/Desktop/ClickImage-3.png")

    clickslabel = Label(clickgame, text="0 Clicks")
    clickButton = Button(clickgame, image=Background_btn, command=buttonCommand, borderwidth=0)
    clickmultX1 = Button(clickgame, text="Buy Click Multiplyer x1 (Costs 100C)", command=multiplyerX1, padx = 10, pady = 5)
    clickmultX5 = Button(clickgame, text="Buy Click Multiplyer x5 (Costs 500C)", command=multiplyerX5, padx = 10, pady = 5)
    clickmultX10 = Button(clickgame, text="Buy Click Multiplyer x10 (Costs 10000C)", command=multiplyerX10, padx = 10, pady = 5)

    clickslabel.pack()
    clickButton.pack()
    clickmultX1.pack()
    clickmultX5.pack()
    clickmultX10.pack()

    clickgame.mainloop()

Second File:

from tkinter import *
import os
import sys
import ClickingGame

mult = 1
clicks = 0

menu = Tk()

q = 0

def firegame():
    global q
    ClickingGame.Game()

TitleLabel = Label(menu, text='Welcome To Techno Play')
GameLabel = Label(menu, text='Games:')
ClickingGameButton = Button(menu, text='Clicking Game', command=firegame)
                            
TitleLabel.pack()
GameLabel.pack()
ClickingGameButton.pack()

Everytime I run this I get a error saying _tkinter.TclError: image "pyimage1 doesn't exist". But the first file runs fine if I run it alone. I am using python idle 3.8.2.

martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

0

You have two Tk() created when your script is running, menu = Tk() and clickgame = Tk(), try chaging the one inside your first file to Toplevel() and delete clickgame.mainloop() since you change clickgame to a Toplevel of your main GUI.

In the first file:

def Game(menu):

    mult = 1
    clicks = 0
    clickgame = Toplevel(menu)
    ...
    ...

In the second file just make menu a global variable, right before menu = Tk(), then send this root when you call your function ClickingGame like this ClickingGame.Game(menu) and at the end of the file start the mainloop:

...
...
...
TitleLabel.pack()
GameLabel.pack()
ClickingGameButton.pack()
main.mainloop()
  • Thanks so much, You have saved me so much time. But One error occurred on the second file, code: import os import sys import ClickingGame mult = 1 clicks = 0 global menu menu = Tk() q = 0 def firegame(): global q ClickingGame.Game() TitleLabel = Label(menu, text='Welcome To Techno Play') GameLabel = Label(menu, text='Games:') ClickingGameButton = Button(menu, text='Clicking Game', command=firegame) TitleLabel.pack() GameLabel.pack() ClickingGameButton.pack() menu.mainloop() – Techo WinMC Sep 29 '20 at 23:33
  • What's the error? – Nestor Calvo Sep 29 '20 at 23:44
  • it says menu is not defined – Techo WinMC Sep 29 '20 at 23:48
  • Answer edited, this should work! – Nestor Calvo Sep 30 '20 at 00:04
0

Try putting clickgame = Tk() at the beggining of the file that contains the mainloop() function