Jump to content

Tkinter trouble

I've been working on a simple dice game in python part of a school challenge and am adding a gui. Its not meant to be anything grand, just like minesweeper level, so I'm using tk/tkinter.

I hit an odd issue, when I try to open/import an image it works...kinda

When the function that creates the window and image is on its on script and is called to as shown here, it works with no issue.

Spoiler

import tkinter
from tkinter import *

def rolldispay(): #the function that seems to fail when in the code
    rollpanel = tkinter.Tk()
    rollpanel.title(rolltitle)
    rollpanel.geometry('250x150')
    rollpanel.wm_iconbitmap('avatar.ico')
    rollpanel.configure(background='black')
    img = PhotoImage(file="diceone.gif") #what appears to go missing
    panel = Label(rollpanel, image = img)
    panel.place(x=25,y=10)
    rollpanel.mainloop()
#variables that come from other parts, known to work
playername = 'RedWulf'
rolltitle = (playername,"'s","roll")
#how the function is called in the full code
rolldisplay()

but when I call the function in the game code I get the following error

Quote

Traceback (most recent call last): File "C:\Users\kevin\Documents\python\Visual\Wulves.py", line 333, in rolldisplay() File "C:\Users\kevin\Documents\python\Visual\Wulves.py", line 269, in rolldisplay panel = Label(rollpanel, image = img) File "C:\Program Files\Python35\lib\tkinter__init__.py", line 2605, in init Widget.init(self, master, 'label', cnf, kw) File "C:\Program Files\Python35\lib\tkinter__init__.py", line 2138, in init (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: image "pyimage1" doesn't exist

The function is called with just rolldisplay() and it finds the icon image with no trouble so I'm not sure what the issue is.

I've tried moving the window/image to the main program(instead of using a function) and I tried appending the system path and  giving a full path name.

Thanks

I would list the full code but its something like 300 lines and is still messy since I'm adding the gui to what was a functional text based game.

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[i7-7700k@5Ghz | MSI Z270 M7 | 16GB 3000 GEIL EVOX | STRIX ROG 1060 OC 6G | EVGA G2 650W | ROSEWILL B2 SPIRIT | SANDISK 256GB M2 | 4x 1TB Seagate Barracudas RAID 10 ]

[i3-4360 | mini-itx potato | 4gb DDR3-1600 | 8tb wd red | 250gb seagate| Debian 9 ]

[Dell Inspiron 15 5567] 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, RedWulf said:

I've been working on a simple dice game in python part of a school challenge and am adding a gui. Its not meant to be anything grand, just like minesweeper level, so I'm using tk/tkinter.

I hit an odd issue, when I try to open/import an image it works...kinda

When the function that creates the window and image is on its on script and is called to as shown here, it works with no issue.

  Hide contents

import tkinter
from tkinter import *

def rolldispay(): #the function that seems to fail when in the code
    rollpanel = tkinter.Tk()
    rollpanel.title(rolltitle)
    rollpanel.geometry('250x150')
    rollpanel.wm_iconbitmap('avatar.ico')
    rollpanel.configure(background='black')
    img = PhotoImage(file="diceone.gif") #what appears to go missing
    panel = Label(rollpanel, image = img)
    panel.place(x=25,y=10)
    rollpanel.mainloop()
#variables that come from other parts, known to work
playername = 'RedWulf'
rolltitle = (playername,"'s","roll")
#how the function is called in the full code
rolldisplay()

but when I call the function in the game code I get the following error

The function is called with just rolldisplay() and it finds the icon image with no trouble so I'm not sure what the issue is.

I've tried moving the window/image to the main program(instead of using a function) and I tried appending the system path and  giving a full path name.

Thanks

I would list the full code but its something like 300 lines and is still messy since I'm adding the gui to what was a functional text based game.

I believe i had this problem before. Try disabling the garbage collector by using

	import gc
	gc.disable()
	

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, TheGamingHD said:

I believe i had this problem before. Try disabling the garbage collector by using

 


	import gc
	gc.disable()
	

 

If you have to disable gc for your code to work it means you're doing something wrong.

 

@RedWulf

When you create the PhotoImage try setting the master parameter to rollpanel.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, TheGamingHD said:

I believe i had this problem before. Try disabling the garbage collector by using

 


	import gc
	gc.disable()
	

 

 

58 minutes ago, fizzlesticks said:lf

When you create the PhotoImage try setting the master parameter to rollpanel.

I actually found out my problem, I had a tkinter window open that was supposed to close before the troubled window opens but I had a flow control issue that caused it not to close and two tkinter sessions at once can cause issues like this, thank you both though. 

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[i7-7700k@5Ghz | MSI Z270 M7 | 16GB 3000 GEIL EVOX | STRIX ROG 1060 OC 6G | EVGA G2 650W | ROSEWILL B2 SPIRIT | SANDISK 256GB M2 | 4x 1TB Seagate Barracudas RAID 10 ]

[i3-4360 | mini-itx potato | 4gb DDR3-1600 | 8tb wd red | 250gb seagate| Debian 9 ]

[Dell Inspiron 15 5567] 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×