0

I'm currently working on a hangman project. My code is ready and working. But I'm having some trouble with my images. I want them to be showing under window2 because that is the actual game. But they are not showing and no error. What am I doing wrong?

def fgissning(bokstäver):
    char_knappar[bokstäver].config(state="disabled")
    global antal_gissningar, imglabel, window2
    meddelande = ''
    if antal_gissningar < 10:
      txt = list(ord_mellanrum)
      gissade = list(Okäntord.get())
      if ord_mellanrum.count(bokstäver) > 0:
        for c in range(len(txt)):
          if txt[c] == bokstäver:
            gissade[c] = bokstäver
            Okäntord.set("".join(gissade))
          if Okäntord.get() == ord_mellanrum:
            meddelande = messagebox.showinfo("Räddad gubbe", "Du klarade det!")
        else:
            imglabel.config(image=window2.photos[antal_gissningar])



      else:
          antal_gissningar += 1
          if antal_gissningar == 10:
            meddelande = messagebox.showwarning("Gubben dog", "Spelet slut")

    if meddelande:
        window2.destroy()
        nyttSpel()


def spelet(word):
    global window2, Okäntord, char_knappar, gissad_ord
    #Skapar en toplevel för själva spelet
    window2 = Toplevel()
    window2.title("Rädda Gubben")
    window2.resizable(False, False)
    window2.photos = [PhotoImage(file="images/hang11.png"), PhotoImage(file="images/hang10.png"),
          PhotoImage(file="images/hang9.png"), PhotoImage(file="images/hang8.png"),
          PhotoImage(file="images/hang7.png"), PhotoImage(file="images/hang6.png")]

    imglabel = Label(window2)
    imglabel.grid(row=0, column=0, columnspan=3, padx=10, pady=40)
    imglabel.config(image=window2.photos[0])
    # Bokstäverna till storbokstav
    gissad_ord = word.upper()

PS. Photos and imglabel are in the spelet function just not when i copied it from my computer. Thanks for your help!

  • If the `photos` list is inside the `spelet` function move it outside the function. – JacksonPro Mar 29 '21 at 14:10
  • @JacksonPro hello! but then I get the error message that says too early to create image – Erik Eckner Mar 29 '21 at 14:13
  • Where did you place it? If you have placed it before creating `Tk` instance place it after. – JacksonPro Mar 29 '21 at 14:17
  • @JacksonPro I placed it after the function spelet because I want it to be in window2. Because if I place it under window = Tk() window2 is not defined – Erik Eckner Mar 29 '21 at 14:21
  • I didn't quite understand that message. Here's a simple solution if `photos` is still inside the `spelet` function change `photos` - > `window2.photos` or just make it global – JacksonPro Mar 29 '21 at 14:24
  • @JacksonPro YES! Thank you now it's working. If you have the time, do you know what I need to do so everytime I guess the wrong letter, the picture change? Thank you so much again! – Erik Eckner Mar 29 '21 at 14:30
  • Do you want to iter through the image list or just show an image? – JacksonPro Mar 29 '21 at 14:32
  • @JacksonPro Yes. When I guess the wrong letter I want the image to change to the next image from the photo list and so on until the last one when the game is lost. – Erik Eckner Mar 29 '21 at 14:35
  • few things to do your `imglabel` must be `global` now I am guessing `if txt[c] == letter:` <- this is the condition that checks whether the user has entered the correct letter. so add an else statement where you have `imglabel.config(image=photos[number_of_guesses])` make sure that `number_of_guesses` is smaller than the size of the image list. – JacksonPro Mar 29 '21 at 14:41
  • @JacksonPro Tried it but it doesn't work can you see what's the problem I editet the code so you can see what i've done. – Erik Eckner Mar 29 '21 at 14:51
  • Ask a new question don' edit this one. – JacksonPro Mar 29 '21 at 14:56
  • @JacksonPro ok that will take some time 90 minutes delay – Erik Eckner Mar 29 '21 at 14:58
  • that's fine. please don't edit the existing question to ask a new one – JacksonPro Mar 29 '21 at 15:02
  • @JacksonPro yes sorry! – Erik Eckner Mar 29 '21 at 15:03
  • @JacksonPro the other one should be up now – Erik Eckner Mar 29 '21 at 15:25

0 Answers0