簡體   English   中英

如何使我的功能不能與其他人同時使用?

[英]How do I make my functions not working at the same time with another?

我正在嘗試創建一個運氣游戲,您必須在3個門后挑2個相同顏色的球,然后嘗試3次,每個門有1個球,而我在您打開一扇門時被擋住了,我想在門顯示出球的那一刻和球消失的那一刻之間延遲一下。 我已經嘗試過使用time.sleep但是它會在顯示器運行時進入睡眠狀態。 這是我的代碼:

import tkinter as tk
import tkinter as tk
from random import shuffle
import time
fenetre = tk.Tk()
fenetre['bg']='black'
fenetre.geometry("1152x768")
color = ["red", "green", "yellow"]
shuffle(color)

frameGauche = tk.Frame(width=200, height=600, bg='pink')
frameGauche.grid(row=0, column=0, padx=10, pady=10)

frameDroite = tk.Frame(width=700, height=700, bg='grey')
frameDroite.grid(row=0, column=1, padx=10, pady=10)

portegauche=tk.Frame(frameDroite, width=200, 
height=600,bg='white',bd=5,relief='groove')
portegauche.grid(row=0, column=0, padx=5, pady=5)

portemilieu=tk.Frame(frameDroite, width=200, 
height=600,bg='white',bd=5,relief='groove')
portemilieu.grid(row=0, column=1, padx=5, pady=5)

portedroite=tk.Frame(frameDroite, width=200, 
height=600,bg='white',bd=5,relief='groove')
portedroite.grid(row=0, column=2, padx=5, pady=5)

def show1(canvas1, bouton2, bouton3):
    canvas1.grid(row=0, column=1)
    bouton2['state']='disabled'
    bouton3['state']='disabled'
    time.sleep(2)
    bouton2['state']='normal'
    bouton3['state']='normal'
    canvas1.grid_remove()
def show2():
    canvas2.grid(row=0, column=2)
    bouton1['state']='disabled'
    bouton3['state']='disabled'
    time.sleep(2)
    bouton1['state']='normal'
    bouton3['state']='normal'
    canvas2.grid_remove()
def show3():
    canvas3.grid(row=0, column=3)
    bouton2['state']='disabled'
    bouton1['state']='disabled'
    time.sleep(2)
    bouton2['state']='normal'
    bouton1['state']='normal'
    canvas3.grid_remove()

canvas1=tk.Canvas(portegauche,width=200, height=600, bg='white')
c1 = canvas1.create_oval((60,280), (140,340), width=1, outline="black", 
fill=color[0])
canvas1.grid_forget()

canvas2=tk.Canvas(portemilieu,width=200, height=600, bg='white')
c2 = canvas2.create_oval((60,280), (140,340), width=1, outline="black", 
fill=color[1])
canvas2.grid_forget()

canvas3=tk.Canvas(portedroite,width=200, height=600, bg='white')
c3 = canvas3.create_oval((60,280), (140,340), width=1, outline="black", 
fill=color[2])
canvas3.grid_forget()

def recommencer():
    canvas1.grid_remove()
    canvas2.grid_remove()
    canvas3.grid_remove()
    shuffle(color)
    canvas1.create_oval((60,280), (140,340), width=1, outline="black", fill=color[0])
    canvas2.create_oval((60,280), (140,340), width=1, outline="black", fill=color[1])
    canvas3.create_oval((60,280), (140,340), width=1, outline="black", fill=color[2])
    bouton1['state']='normal'
    bouton2['state']='normal'
    bouton3['state']='normal'

boutonR = tk.Button(frameGauche, text='Recommencer',command=recommencer)
boutonR.grid(row=0, column=0, padx=50, pady=50)

bouton1=tk.Button(frameDroite, text= 'Ouvrir',command=lambda: show1(canvas1, 
bouton2, bouton3))
bouton1.grid(row=1, column=0)

bouton2=tk.Button(frameDroite, text= 'Ouvrir',command=show2)
bouton2.grid(row=1, column=1)

bouton3=tk.Button(frameDroite, text= 'Ouvrir',command=show3)
bouton3.grid(row=1, column=2)

fenetre.mainloop()

如前所述,sleep()將凍結執行,在GUI編程中絕不是一個好主意。

為了使此工作正常進行,請將showx函數分解為show和hide,這里是show1的示例。 在這里,我們使用.after調用lambda函數(這使傳遞參數更容易),. after的第一個參數是毫秒級的時間

def show1(canvas1, bouton2, bouton3):
    canvas1.grid(row=0, column=1)
    bouton2['state']='disabled'
    bouton3['state']='disabled'
    # OLD fenetre.after(2000, lambda: hide1(canvas1, bouton2, bouton3))
    # New thanks to Mike-SMT
    fenetre.after(2000, hide1, canvas1, bouton2, bouton3)

def hide1(canvas1, bouton2, bouton3):
    bouton2['state']='normal'
    bouton3['state']='normal'
    canvas1.grid_remove()

在這里我從Tkinter得到了一些細節的答案

[EDIT]您可以通過創建一個show和一個hide函數並將畫布/按鈕對象作為參數進行傳遞,從而大大簡化這一過程,從長遠來看,還有更多的重構方法可以簡化此過程,但是您可以開始使用。 after()應該是您現在需要的。

您可能要刪除文件頂部的雙重導入

import tkinter as tk

主要問題是您對sleep() tkinter中的睡眠會導致整個實例凍結,並且無法按預期工作。 相反,您可以使用after()

另外,您將兩次導入tkinter。 從tkinter的進口商品中刪除。

也就是說,我們需要添加一個新功能並在您的功能中更改幾行。

我添加了一個名為normalize_button()的函數,該函數以1個參數作為按鈕名稱。 它與after()方法結合使用,以在2秒后更新按鈕。

要在評論中回答您的問題:

經過2秒鍾后, after()方法將調用normalize_button()函數。 該方法檢查傳遞給它的字符串,並將基於該字符串更新按鈕。 實際上,您可以使用show方法執行相同的操作,並且只有一種方法可以根據需要更新所有按鈕。 它使事情更整潔,並遵循DRY(不要重復自己)PEP8樣式。

看下面的代碼。

import tkinter as tk
from random import shuffle

fenetre = tk.Tk()
fenetre['bg']='black'
fenetre.geometry("1152x768")
color = ["red", "green", "yellow"]
shuffle(color)

frameGauche = tk.Frame(width=200, height=600, bg='pink')
frameGauche.grid(row=0, column=0, padx=10, pady=10)

frameDroite = tk.Frame(width=700, height=700, bg='grey')
frameDroite.grid(row=0, column=1, padx=10, pady=10)

portegauche=tk.Frame(frameDroite, width=200, 
height=600,bg='white',bd=5,relief='groove')
portegauche.grid(row=0, column=0, padx=5, pady=5)

portemilieu=tk.Frame(frameDroite, width=200, 
height=600,bg='white',bd=5,relief='groove')
portemilieu.grid(row=0, column=1, padx=5, pady=5)

portedroite=tk.Frame(frameDroite, width=200, 
height=600,bg='white',bd=5,relief='groove')
portedroite.grid(row=0, column=2, padx=5, pady=5)

def normalize_button(btn_name):
    print(btn_name)
    if btn_name == "b1":
        bouton1['state']='normal'
    if btn_name == "b2":
        bouton2['state']='normal'
    if btn_name == "b3":
        bouton3['state']='normal'

def show1():
    canvas1.grid(row=0, column=1)
    bouton2['state']='disabled'
    bouton3['state']='disabled'
    fenetre.after(2000, normalize_button, "b2")
    fenetre.after(2000, normalize_button, "b3")
    fenetre.after(2000, canvas1.grid_forget)

def show2():
    canvas2.grid(row=0, column=2)
    bouton1['state']='disabled'
    bouton3['state']='disabled'
    fenetre.after(2000, normalize_button, "b1")
    fenetre.after(2000, normalize_button, "b3")
    fenetre.after(2000, canvas2.grid_forget)

def show3():
    canvas3.grid(row=0, column=3)
    bouton2['state']='disabled'
    bouton1['state']='disabled'
    fenetre.after(2000, normalize_button, "b2")
    fenetre.after(2000, normalize_button, "b1")
    fenetre.after(2000, canvas3.grid_forget)

canvas1 = tk.Canvas(portegauche,width=200, height=600, bg='white')
c1 = canvas1.create_oval((60,280), (140,340), width=1, outline="black", fill=color[0])
canvas1.grid_forget()

canvas2 = tk.Canvas(portemilieu,width=200, height=600, bg='white')
c2 = canvas2.create_oval((60,280), (140,340), width=1, outline="black", fill=color[1])
canvas2.grid_forget()

canvas3 = tk.Canvas(portedroite,width=200, height=600, bg='white')
c3 = canvas3.create_oval((60,280), (140,340), width=1, outline="black", fill=color[2])
canvas3.grid_forget()

def recommencer():
    canvas1.grid_remove()
    canvas2.grid_remove()
    canvas3.grid_remove()
    shuffle(color)
    canvas1.create_oval((60,280), (140,340), width=1, outline="black", fill=color[0])
    canvas2.create_oval((60,280), (140,340), width=1, outline="black", fill=color[1])
    canvas3.create_oval((60,280), (140,340), width=1, outline="black", fill=color[2])
    bouton1['state']='normal'
    bouton2['state']='normal'
    bouton3['state']='normal'

boutonR = tk.Button(frameGauche, text='Recommencer',command=recommencer)
boutonR.grid(row=0, column=0, padx=50, pady=50)

bouton1=tk.Button(frameDroite, text= 'Ouvrir',command=show1)
bouton1.grid(row=1, column=0)

bouton2=tk.Button(frameDroite, text= 'Ouvrir',command=show2)
bouton2.grid(row=1, column=1)

bouton3=tk.Button(frameDroite, text= 'Ouvrir',command=show3)
bouton3.grid(row=1, column=2)

fenetre.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM