Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关于如何停止我的按钮点击tkinter的问题?_Python_Tkinter - Fatal编程技术网

Python 关于如何停止我的按钮点击tkinter的问题?

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

基本上,我正在为学校制作一个非常基本的python游戏,我只是想尽快完成这个游戏,
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()