我在使用标签和变量Tkinter python 3时遇到问题

时间:2020-01-24 22:19:49

标签: python-3.x tkinter

我正在实施一个Rock Paper Scissors游戏,在该游戏中您要与RNG进行对抗,一切都很好,但是我添加Tkinter是为了娱乐,我真的想了解我在这里缺少的东西。

我能够在命令行上打印我想要的结果,但不能在创建的窗口上打印。

希望了解如何在Tkinter窗口中打印变量enemy

import tkinter
import random


# Let's create the Tkinter window.
window = tkinter.Tk()
window.title("Paper Scissor Rock")




brains = ["ROCK" ,"PAPER", "SCISSOR"]

def enter_rock():
    click = tkinter.Label(window, text = "You picked ROCK   ")
    enemy = random.choice(brains)
    print("your enemy picked  "+ enemy )
    click.pack()

    if enemy == "ROCK":

        print("DRAW")

    elif enemy == "PAPER":

        print("YOU LOSE")

    else:
        print("YOU WIN")


def enter_paper():
    click = tkinter.Label(window, text = "You picked PAPER  ")
    #paper = 1
    enemy = random.choice(brains)
    print("your enemy picked  "+ enemy )
    click.pack()

    if enemy == "ROCK":

        print("YOU WIN")

    elif enemy == "PAPER":

        print("DRAW")

    else:

        print("YOU LOSE")

def enter_scissor():
    click = tkinter.Label(window, text = "You picked SCISSOR ")
    #scissor = 1
    enemy = random.choice(brains)
    print("your enemy picked  "+ enemy )
    click.pack()

    if enemy == "ROCK":

        print("YOU LOSE")

    elif enemy == "PAPER":

        print("YOU WIN")

    else:
        print("DRAW")




# Once the frames are created then you are all set to add widgets in both the frames.
rock_b = tkinter.Button(window, text = "rock", fg = "red", padx=50, pady= 50, command = enter_rock) #'fg or foreground' is for coloring the contents (buttons)

paper_b = tkinter.Button(window, text = "paper", fg = "green", padx=50, pady= 50,command = enter_paper)

scissor_b = tkinter.Button(window, text = "scissor", fg = "purple", padx=50, pady= 50, command = enter_scissor)


rock_b.pack()

paper_b.pack()

scissor_b.pack()

2 个答案:

答案 0 :(得分:0)

我认为这更像您的想法。

import tkinter
import random

brains = ["ROCK" ,"PAPER", "SCISSOR"]

def show_first (text) :
    answer.config (state = "normal")
    answer.delete (1.0, 'end')
    answer.insert ('end', text + '\n')

def show_second (text) :
    answer.insert ('end', text)
    answer.config (state = "disabled")

def enter_rock():
    enemy = random.choice(brains)
    show_first ("your enemy picked  "+ enemy)
    if enemy == "ROCK":
        show_second ("DRAW")
    elif enemy == "PAPER":
        show_second ("YOU LOSE")
    else:
        show_second ("YOU WIN")

def enter_paper():
    enemy = random.choice(brains)
    show_first ("your enemy picked  "+ enemy )
    if enemy == "ROCK":
        show_second ("YOU WIN")
    elif enemy == "PAPER":
        show_second ("DRAW")
    else:
        show_second ("YOU LOSE")

def enter_scissor():
    enemy = random.choice(brains)
    show_first ("your enemy picked  "+ enemy )
    if enemy == "ROCK":
        show_second ("YOU LOSE")
    elif enemy == "PAPER":
        show_second ("YOU WIN")
    else:
        show_second ("DRAW")

# Let's create the Tkinter window.
window = tkinter.Tk()
window.title("Paper Scissor Rock")
rock = tkinter.Button (window, text = "You picked ROCK   ")
rock.config (command = enter_rock)
rock.pack ()
paper = tkinter.Button (window, text = "You picked PAPER  ")
paper.config (command = enter_paper)
paper.pack ()
scissor = tkinter.Button (window, text = "you picked SCISSOR")
scissor.config (command = enter_scissor)
scissor.pack ()
answer = tkinter.Text (window, width = 30, height = 3)
answer.pack ()

window.mainloop ()

答案 1 :(得分:0)

在此之前我做了很多识别错误,我认为哪里会出现另一个错误,这是个大问题,所以这就是我想要的,只是打印出所选择的内容并 谁赢了:

def enter_rock():
    enemy = random.choice(brains)
    click = tkinter.Label(window, text = "You picked ROCK your enemy picked  " + enemy)
    click.pack()

    if enemy == "ROCK":
      result = tkinter.Label(window, text = "DRAW")
      result.pack()

    elif enemy == "PAPER":
      result = tkinter.Label(window, text = "YOU LOSE")
      result.pack()

    else:
      result = tkinter.Label(window, text = "YOU WIN")
      result.pack()