Python 关于如何停止我的按钮点击tkinter的问题?
基本上,我正在为学校制作一个非常基本的python游戏,我只是想尽快完成这个游戏,Python 关于如何停止我的按钮点击tkinter的问题?,python,tkinter,Python,Tkinter,基本上,我正在为学校制作一个非常基本的python游戏,我只是想尽快完成这个游戏,def RNG():在我得到一个随机数的按钮后,我如何停止按钮?我不能点击它 import tkinter from tkinter import * class Window(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.master = master root
def RNG():
在我得到一个随机数的按钮后,我如何停止按钮?我不能点击它
import tkinter
from tkinter import *
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
root = Tk( )
app = Window(root)
root.title("BlackJack Game")
root.geometry("1920x1080+0+0")
root.configure(bg='gold')
lbl: Label = tkinter.Label(root, text='Welcome to Blackjack!', fg='steelblue', bg='gold1',
font=('arial', 50, "bold"))
lbl.pack()
def open():
global top
top = Toplevel()
top.configure(bg='green')
top.geometry("1920x1080+0+0")
top.title('Game Window')
lbl_1 = Label(top, text="BlackJack\n ", font=("arial", 75, "bold"), fg="black", bg="green").pack()
root.withdraw()
def RNG():
import random
x = random.randint(2, 21)
lab = Label(top, text=x, font=("arial", 20, "bold"), fg="Black", bg='brown')
lab.pack()
global but
but = Button(top, text='test', font=("arial", 20, "bold"), fg="Black", bg='brown', command=RNG, height=7, width=18).pack()
work = Button(root, text="Press Play: ", font=("arial", 20, "bold"), fg="Black", bg='orange', command=open, ).pack()
root.mainloop()
你可以用几种方法来做到这一点 首先:您可以将按钮的状态设置为禁用
but = Button(top, text='test', state = DISABLED, font=("arial", 20, "bold"), fg="Black", bg='brown', command=RNG, height=7, width=18).pack()
或
请注意,您应该禁用它正在调用的函数中的按钮。(为了简单起见)
秒:您可以在之后重新定义按钮,并将其调用的命令更改为“”
”
这样就不能对其进行垃圾邮件处理,这也意味着当您调用
open()
时,它应该将但是按钮更改回正常状态。您可以通过几种方法来实现这一点
首先:您可以将按钮的状态设置为禁用
but = Button(top, text='test', state = DISABLED, font=("arial", 20, "bold"), fg="Black", bg='brown', command=RNG, height=7, width=18).pack()
或
请注意,您应该禁用它正在调用的函数中的按钮。(为了简单起见)
秒:您可以在之后重新定义按钮,并将其调用的命令更改为“”
”
这样就不能对其进行垃圾邮件处理,这也意味着当您调用open()
时,它应该将按钮改回正常状态。您所说的“停止按钮”是什么意思?“垃圾邮件点击它”是什么意思?您可以通过but.config(state='disabled')
函数中的RNG()
禁用按钮,但需要将行but=button(top,text='test',…).pack()
分为两条语句:but=button(…)
和but.pack()
,您应该尽量避免从函数内部导入。把我们所有的导入都放在文件的开头是一个好习惯。你说的“停止按钮”是什么意思?“垃圾邮件点击它”是什么意思?您可以通过but.config(state='disabled')
函数中的RNG()
禁用按钮,但需要将行but=button(top,text='test',…).pack()
分为两条语句:but=button(…)
和but.pack()
,您应该尽量避免从函数内部导入。将所有导入内容放在文件开头是一个好习惯。
but = Button(top, text='test', state = DISABLED, font=("arial", 20, "bold"), fg="Black", bg='brown', command="", height=7, width=18).pack()