#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Help with rock paper scissors gui


    Hello. I'm a beginner in Python and have a lot of trouble making this game work. I have made pictures for rock paper and scissors so the user can click on them to choose them. At the beginning the user is asked how much points he wants to play to.
    In the end I want to show with a Label the result, but i can not manage to do that correctly. Here is the code:




    import ImageTk, random, Tkinter, sys
    from Tkinter import *
    from winsound import PlaySound, SND_FILENAME, SND_ASYNC


    program = Tk()

    program.title('Sten Sax Påse')
    program.geometry('700x500+450+100')

    bild1 = ImageTk.PhotoImage(file = 'sten.jpg')
    bild2 = ImageTk.PhotoImage(file = 'sax.jpg')
    bild3 = ImageTk.PhotoImage(file = 'pose.jpg')




    class Inlamningsuppgift(Frame):
    def __init__(self, master):
    Frame.__init__(self, master)
    self.grid()
    self.skapa_design()



    def skapa_design(self):
    self.label1 = Label(self, text = "Välkommen till spelet: Sten sax påse!\n"
    , font = 20, fg = 'Blue').grid(row = 0, column = 1, sticky = W)

    self.label2 = Label(self, text = "Hur mycket poäng vill du spela till?\n"
    ).grid(row = 1, column = 0, sticky = E)

    self.entry = Entry(self)
    self.entry.grid(row = 1, column = 1)

    self.knapp1 = Button(self, text = 'Ok, börja spela', command = self.inmatning
    ).grid(row = 1, column = 1, sticky = E)


    def inmatning(self):
    self.slutpoang = int(self.entry.get())

    if self.slutpoang < 1:
    pass
    else:
    self.spela()

    def spela(self):
    self.label3 = Label(self, text="Välj ditt alternativ mot datorn\n", font = 20
    ).grid(row = 2, column = 1)
    self.stenknapp = Button(self, width = 200, height = 200, image = bild1,
    text="Sten", bg='green', command = self.klicksten)

    self.stenknapp.grid(row = 3, column = 0)


    self.saxknapp = Button(self, width = 200, height = 200, image = bild2,
    text="Sax", bg='green', command = self.klicksax)

    self.saxknapp.grid(row = 3, column = 1)


    self.poseknapp = Button(self, width = 200, height = 200, image = bild3,
    text="Påse", bg='green', command = self.klickpose)

    self.poseknapp.grid(row = 3, column = 2)


    def klicksten(self):
    self.anvandare = 1
    self.utrakning()



    def klicksax(self):
    self.anvandare = 2
    self.utrakning()


    def klickpose(self):
    self.anvandare = 3
    self.utrakning()


    def testresultat(self):


    if self.anvandare == self.dator:
    self.anvmotdator = 0

    elif (self.anvandare==1 and self.dator==2) or (self.anvandare==2 and self.dator==3) or (self.anvandare==3 and self.dator==1):
    self.anvmotdator = 1

    else:
    self.anvmotdator = 2

    return self.anvmotdator


    def utrakning(self):

    self.m = 0
    self.d = 0
    while self.m < self.slutpoang and self.d < self.slutpoang:

    self.dator = random.randint(1,4)
    self.testresultat()
    if self.anvmotdator == 1:
    self.m = self.m+1
    self.pluspoang = Label(self, text="Grattis, du vann en poäng mot datorns"
    ).grid(row=5, column = 1)

    elif self.anvmotdator == 2:
    self.d = self.d+1
    self.datorpoang = Label(self, text="Tyvärr, datorn vann en poäng med"
    ).grid(row = 5, column = 1)

    else:
    self.lika = Label(self, text="Oavgjort! Fortsätt spela").grid(row = 5, column = 1)

    self.stallning = Label(self, text="-------------------\n Spelare mot dator:"
    " " + str(self.m) + " " + str(self.d)).grid(row = 6, column = 1)


    app = Inlamningsuppgift(program)
    program.mainloop()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    530
    Rep Power
    34
    You attach a Tkinter StringVar to the Label, instead of text=, and set the StringVar to the new string.
    Code:
    from Tkinter import *
    root=Tk()
    
    def change_label():
        label_str.set("This is a new string ")
    
    label_str=StringVar()
    Label(root, textvariable=label_str).grid()
    label_str.set("Beginning string")
    Button(root, text='Click Me', command=change_label).grid(row=1)
    root.mainloop()

IMN logo majestic logo threadwatch logo seochat tools logo