繁体   English   中英

使用tkinter获取矩形以在文本上绘制

[英]Getting a rectangle to draw over text with tkinter

我正在尝试制作剪刀石头布游戏,但我不知道如何在文本上绘制矩形。 这是我的代码:

class game:
    def __init__(self):
        self.choices = ['Rock','Paper','Scissors']
        self.final_human_score = 0
        self.final_computer_score = 0
        rock_b = Button(canvas,text="Rock",bg='black',fg='green',command=self.rock)
        rock_b.place(x=225,y=40)
        paper_b = Button(canvas,text="Paper",bg='black',fg='green',command=self.paper)
        paper_b.place(x=290,y=40)
        scissors_b = Button(canvas,text="Scissors",bg="black",fg='green',comman=self.scissors)
        scissors_b.place(x=360,y=40)
        self.human_turn = False
        self.computer_turn = True

    def turn(self):
        start_x = 75
        start_y = 100
        start_x2 = 90
        start_y2 =200
        Turn = canvas.create_rectangle(start_x,start_y,50,50,fill='green')
        not_turn = canvas.create_rectangle(start_x2,start_y2,50,50,fill='red')

    def rock(self):
        if self.human_turn == False:
            #280,90,400,113
            self.choice_display = canvas.create_text(310,100,text="Rock",fill="white",font=('Courier',13))
            global human_turn #global required to change human_turn and commputer_turn
            global computer_turn
            self.human_turn = True
            self.computer_turn = False
            self.cc()

    def paper(self):
        if self.human_turn == False:
            self.choice_display = canvas.create_text(315,100,text="Paper",fill="white",font=('Courier',13))
            global human_turn
            global computer_turn
            self.human_turn = True
            self.computer_turn = False
            self.cc()

    def scissors(self):
        if self.human_turn == False:
            self.choice_display = canvas.create_text(330,100,text="Scissors",fill="white",font=('Courier',13))
            global human_turn
            global computer_turn
            self.human_turn = True
            self.computer_turn = False
            self.cc()

    def cc(self): #(computer choice)
        computer_choice = random.choice(self.choices)
        if self.computer_turn == False:
            if computer_choice == 'Rock':
                self.c_choice_display = canvas.create_text(310,200,text="Rock",fill="white",font=("Courier",13))
                global human_turn
                global computer_turn
                self.human_turn = False
                self.computer_turn = True
            elif computer_choice == 'Paper':
                self.c_choice_display = canvas.create_text(315,200,text="Paper",fill="white",font=("Courier",13))
                global human_turn
                global computer_turn
                self.human_turn = False
                self.computer_turn = True
            elif computer_choice == 'Scissors':
                self.c_choice_display = canvas.create_text(330,200,text="Scissors",fill="white",font=("Courier",13))
                global human_turn
                global computer_turn
                self.human_turn = False
                self.computer_turn = True

在游戏课中,我如何让它等待3秒钟,然后掩盖计算机的选择和人类的选择?

从Tk手册中了解画布小部件中的窗口:

注意:由于管理窗口的方式的限制,无法在窗口项的顶部绘制其他图形项(例如线条和图像)。 窗口项始终会遮盖与其重叠的所有图形,无论它们在显示列表中的顺序如何。

您的解决方案可以使用可以相互重叠的画布绘制元素。 我没有更改turn方法的工作方式,但是程序中存在泄漏。 您应该在init ()中创建一次overlay元素,然后可以在画布上升高,降低或移动它们。 但是turn方法每次调用都会创建一个新的矩形。

from tkinter import *
import random

class game(Canvas):
    def __init__(self, parent):
        Canvas.__init__(self, parent)
        self.choices = ['Rock','Paper','Scissors']
        self.final_human_score = 0
        self.final_computer_score = 0
        font = ('TkTextFont', 12)
        rock_b = self.create_rectangle(50, 40, 120, 80, fill='black', tags='rock')
        rock_t = self.create_text(85, 60, text='Rock', fill='green', font=font, tags=('rock','trock'))
        paper_b = self.create_rectangle(150, 40, 220, 80, fill='black', tags='paper')
        paper_t = self.create_text(185, 60, text='Paper', fill='green', font=font, tags=('paper','tpaper'))
        scissors_b = self.create_rectangle(250, 40, 320, 80, fill='black', tags='scissors')
        scissors_t = self.create_text(285, 60, text='Scissors', fill='green', font=font, tags=('scissors','tscissors'))
        self.tag_bind('all', '<Enter>', self.activateElement)
        self.tag_bind('all', '<Leave>', self.deactivateElement)
        self.tag_bind('rock', '<Button-1>', self.rock)
        self.tag_bind('paper', '<Button-1>', self.paper)
        self.tag_bind('scissors', '<Button-1>', self.scissors)
        # Create the choice text elements
        self.choice_display = self.create_text(185,100,text="",fill="white",font=('Courier',13))
        self.c_choice_display = self.create_text(185,200,text="",fill="white",font=("Courier",13))
        self.human_turn = False
        self.computer_turn = True

    def activateElement(self, event):
        tag = self.gettags('current')[0]    # This returns rock, paper, or scissors
        text = self.find_withtag('t' + tag)[0]  # This returns the text element associated with the button
        self.itemconfigure(tag, fill='gray20')
        self.itemconfigure(text, fill='yellow')
        self.config(cursor="hand2")

    def deactivateElement(self, event):
        tag = self.gettags('current')[0]    # This returns rock, paper, or scissors
        text = self.find_withtag('t' + tag)[0]  # This returns the text element associated with the button
        self.itemconfigure(tag, fill='black')
        self.itemconfigure(text, fill='green')
        self.config(cursor="arrow")

    def turn(self):
        start_x = 75
        start_y = 100
        start_x2 = 90
        start_y2 =200
        Turn = self.create_rectangle(start_x,start_y,50,50,fill='green')
        not_turn = self.create_rectangle(start_x2,start_y2,50,50,fill='red')

    def rock(self, event):
        if self.human_turn == False:
            #280,90,400,113
            self.itemconfigure(self.choice_display, text='Rock')
            global human_turn #global required to change human_turn and commputer_turn
            global computer_turn
            self.human_turn = True
            self.computer_turn = False
            self.cc()

    def paper(self, event):
        if self.human_turn == False:
            self.itemconfigure(self.choice_display, text='Paper')
            global human_turn
            global computer_turn
            self.human_turn = True
            self.computer_turn = False
            self.cc()

    def scissors(self, event):
        if self.human_turn == False:
            self.itemconfigure(self.choice_display, text='Scissors')
            global human_turn
            global computer_turn
            self.human_turn = True
            self.computer_turn = False
            self.cc()

    def cc(self): #(computer choice)
        computer_choice = random.choice(self.choices)
        if self.computer_turn == False:
            if computer_choice == 'Rock':
                self.itemconfigure(self.choice_display, text='Rock')
                global human_turn
                global computer_turn
                self.human_turn = False
                self.computer_turn = True
            elif computer_choice == 'Paper':
                self.itemconfigure(self.choice_display, text='Paper')
                global human_turn
                global computer_turn
                self.human_turn = False
                self.computer_turn = True
            elif computer_choice == 'Scissors':
                self.itemconfigure(self.choice_display, text='Scissors')
                global human_turn
                global computer_turn
                self.human_turn = False
                self.computer_turn = True

root = Tk()
app = game(root)
app.pack()
root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM