岩石纸剪与python tkinter的比赛

时间:2015-12-13 07:34:42

标签: python tkinter

我想使用面向对象分离GUI和游戏逻辑,但是当我试图显示" self.result"使用tkinter.Label它的节目" PY VAR1"。 任何人都可以帮助如何更新self.result?

rockpaperscissor.py

import random


class RockPaperScissor:
    def __init__(self):
        self.rock = 'rock'
        self.paper = 'paper'
        self.scissor = 'scissor'
        self.playerA_win = 'player A win'
        self.playerB_win = 'player B win'
        self.draw = 'Its draw'
        self.playerA_score = 0
        self.playerB_score = 0
        self.choices = {'rock': self.rock,
                    'paper': self.paper,
                    'scissor': self.scissor}
    self.transition = {
                  (self.rock, self.rock): self.draw,
                  (self.rock, self.paper): self.playerB_win,
                  (self.rock, self.scissor): self.playerA_win,
                  (self.paper, self.rock): self.playerA_win,
                  (self.paper, self.paper): self.draw,
                  (self.paper, self.scissor): self.playerB_win,
                  (self.scissor, self.rock): self.playerB_win,
                  (self.scissor, self.paper): self.playerA_win,
                  (self.scissor, self.scissor): self.draw}

    def play(self, playerA, playerB):
        self.playerA_answer = self.choices[playerA]
        self.playerB_answer = self.choices[playerB]
        self.result = self.transition[self.playerA_answer,
                                  self.playerB_answer]

    def get_result(self):
        return self.result

def computer():
    choices = ('rock', 'paper', 'scissor')
    playerB = random.choice(choices)
    return playerB


if __name__ == "__main__":
    rps = RockPaperScissor()
    while True:
        playerA = input('What is your guess? ')
        playerB = computer()
        rps.play(playerA,playerB)
        print(rps.get_result())

rockpaperscissor_gui.py

import tkinter as tk
import rockpaperscissor as rps


class MyFrame(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.pack()
        self.value = tk.StringVar()
        self.result = tk.StringVar()
        self.choice = ('rock', 'paper', 'scissor')
        self.rps = rps.RockPaperScissor()
        for i in self.choice:
            self.radio = tk.Radiobutton(self,
                                        text=i,
                                        variable=self.value,
                                        value=i).pack()
        self.button = tk.Button(self,
                                text='play',
                                command=self.play).pack()
        self.label = tk.Label(self,
                              text=self.result).pack()

    def play(self):
        playerA = self.value.get()
        playerB = rps.computer()
        self.rps.play(playerA, playerB)
        self.result.set('result: {0}'.format(self.result))

if __name__ == '__main__':
    MyFrame().mainloop()

这是屏幕截图 image

2 个答案:

答案 0 :(得分:0)

感谢现在回答它的工作! 这是新代码

#rockpaperscissor_gui.py
import tkinter as tk
import random as rn
import rockpaperscissor as rps


class RockPaperScissor:
    def __init__(self):
        self.rps = rps.RockPaperScissor()
        self.root = tk.Tk()
        self.playerA = tk.StringVar()
        self.playerA.set('Pick your answer')
        self.playerB = tk.StringVar()
        self.playerB.set('Pick your answer')
        self.result = tk.StringVar()
        self.rps.playerA_score = tk.StringVar()
        self.rps.playerB_score = tk.StringVar()
        self.choices = ('rock', 'paper', 'scissor')
        self.label_choices = (self.playerA, self.playerB, self.result)

        for item in self.label_choices:
            self.label = tk.Label(self.root, textvariable=item)
            self.label.pack()
        for item in self.choices:
            self.radiobutton = tk.Radiobutton(self.root, text=item,
                                              variable=self.playerA,
                                              value=item)
            self.radiobutton.pack()

        self.button = tk.Button(self.root,
                                text='update',
                                command=self.update).pack()
        self.root.mainloop()

    def update(self):
        self.playerB_answer = rn.choice(self.choices)
        self.playerB.set(self.playerB_answer)
        player = self.playerA.get()
        computer = self.playerB.get()
        self.rps.play(player, computer)
        answer = self.rps.get_result()
        self.result.set(answer)


if __name__ == '__main__':
    RockPaperScissor()

答案 1 :(得分:0)

您必须在n+m中使用textvariable并显示Label

顺便说一句:如果您不使用此变量,则不必将小部件分配给变量。

代码可能是这样的

self.rps.get_result()