Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 3.x 函数运行时,Python Tkinter禁用按钮_Python 3.x_Button_Tkinter - Fatal编程技术网

Python 3.x 函数运行时,Python Tkinter禁用按钮

Python 3.x 函数运行时,Python Tkinter禁用按钮,python-3.x,button,tkinter,Python 3.x,Button,Tkinter,除非在执行函数时再次按下按钮,否则以下代码将按预期工作。我尝试禁用功能中的按钮,并在功能完成时重新启用它,但它不起作用。我需要做些什么才能让它工作 import tkinter as tk from tkinter import scrolledtext root = tk.Tk() root.title('Test Case') root.geometry('850x300') root.configure(background='ivory3') label_fill = tk.Labe

除非在执行函数时再次按下按钮,否则以下代码将按预期工作。我尝试禁用功能中的按钮,并在功能完成时重新启用它,但它不起作用。我需要做些什么才能让它工作

import tkinter as tk
from tkinter import scrolledtext

root = tk.Tk()
root.title('Test Case')
root.geometry('850x300')
root.configure(background='ivory3')

label_fill = tk.Label(root, width = "80", height = "1", bg = 'ivory3')
label_fill.grid(row=1, columnspan=3)

textw = scrolledtext.ScrolledText(root, width=18, height=2)
textw.grid(column=0, row=2, sticky='nsew')
textw.tag_configure('tag-left', justify='left')
textw.config(background='light grey', foreground='green',
             font='arial 60 bold', wrap='word', relief='sunken', bd=5)


def func_ex(count=None):

    btn = tk.Button(state=tk.DISABLED)   

    if count is not None:
        if count <= 31:
            if (count % 3) == 1:
                txt = 'START'
                sleep = 2000               
            if (count % 3) == 2:
                txt = 'HOLD'
                sleep = 5000            
            if (count % 3) == 0:
                txt = 'END'
                sleep = 1000             
            if count == 31:
                txt = 'DONE'
                sleep = 1
            textw.delete('1.0', 'end')
            textw.insert('end', txt, 'tag-left')
            count += 1
            root.after(sleep, lambda: func_ex(count))
    else:
        func_ex(1)

    btn = tk.Button(state=tk.NORMAL)   


btn = tk.Button(root, text='Start Test Case', bg='light grey',
                width=18,font='arial 12', relief='raised', bd=5,
                command=func_ex)
btn = btn.grid(row=0, column=0)

root.mainloop()
将tkinter作为tk导入
从tkinter导入滚动文本
root=tk.tk()
root.title('测试用例')
根几何体('850x300')
root.configure(background='ivory3')
标签\u fill=tk.标签(根,宽=80,高=1,背景='ivory3')
标签填充网格(行=1,列span=3)
textw=scrolledtext.scrolledtext(根,宽度=18,高度=2)
textw.grid(列=0,行=2,sticky='nsew')
textw.tag_configure('tag-left',justify='left')
textw.config(背景为淡灰色,前景为绿色,
font='arial 60 bold',wrap='word',relief='sunken',bd=5)
def func_ex(计数=无):
btn=tk.按钮(状态=tk.已禁用)
如果计数不是无:

如果count我已经为您创建了一个解决方案。我所做的最大区别是将应用程序放入一个类中。我发现将tkinter应用程序放在一个类中更容易避免声明顺序问题。你有几个问题,我记下了,但我找不到一种方法来创建一个工作程序,而不把应用程序放在一个类中。欢迎在评论中提出任何问题

将tkinter作为tk导入
从tkinter导入滚动文本
ANAP类:
定义初始化(自):
self.root=tk.tk()
self.root.title('测试用例')
self.root.geometry('850x300')
self.root.configure(background='ivory3')
self.label\u fill=tk.label(self.root,width=“80”,height=“1”,bg='ivory3')
self.label_fill.grid(行=1,列span=3)
self.textw=scrolledtext.scrolledtext(self.root,宽度=18,高度=2)
self.textw.grid(列=0,行=2,sticky='nsew')
self.textw.tag\u configure('tag-left',justify='left'))
self.textw.config(背景为淡灰色,前景为绿色),
font='arial 60 bold',wrap='word',relief='sunken',bd=5)
self.btn=tk.Button(self.root)
self.btn.configure(text='Start Test Case',
bg='浅灰色',宽度=18,
font='arial 12',relief='raised',
bd=5,command=self.func_(ex)
self.btn.grid(row=0,column=0)#grid返回none您的btn=btn.grid,因此您的btn对象变为none
self.count=0
def func_ex(自身):
self.count+=1
txt=”“#初始化变量是最佳实践。。。
睡眠=0
self.btn.configure(state=tk.DISABLED)#您还在函数中创建新的btn对象

如果self.count在查看上面ShayneLoyd代码中的注释后,我能够找到代码中的问题。如上所述,我在函数中创建了一个新按钮。我通过将btn设置为全局并使用.configure更新按钮的状态来修复此问题。下面的代码修复了我的原始代码(在函数运行时可以多次按下按钮)和ShayneLoyd的代码(在函数第一次完成后按下按钮不起任何作用)中的问题

这是工作代码

import tkinter as tk
from tkinter import scrolledtext

root = tk.Tk()
root.title('Test Case')
root.geometry('850x300')
root.configure(background='ivory3')

label_fill = tk.Label(root, width = "80", height = "1", bg = 'ivory3')
label_fill.grid(row=1, columnspan=3)

textw = scrolledtext.ScrolledText(root, width=18, height=2)
textw.grid(column=0, row=2, sticky='nsew')
textw.tag_configure('tag-left', justify='left')
textw.config(background='light grey', foreground='green',
             font='arial 60 bold', wrap='word', relief='sunken', bd=5)


def func_ex(count=None):

    global btn
    btn.configure(state=tk.DISABLED)

    if count is not None:

        if count <= 31:
            if (count % 3) == 1:
                txt = 'START'
                sleep = 2000               
            if (count % 3) == 2:
                txt = 'HOLD'
                sleep = 5000            
            if (count % 3) == 0: 
                txt = 'END'
                sleep = 1000             
            if count == 31:
                txt = 'DONE'
                sleep = 1
            textw.delete('1.0', 'end')
            textw.insert('end', txt, 'tag-left')
            count += 1
            root.after(sleep, lambda: func_ex(count))
        elif count == 32:
             btn.configure(state=tk.NORMAL)
    else:
        func_ex(1)


btn = tk.Button(root, text='Start Test Case', bg='light grey',
                width=18,font='arial 12', relief='raised', bd=5,
                command=func_ex)
btn.grid(row=0, column=0) 

root.mainloop()
将tkinter作为tk导入
从tkinter导入滚动文本
root=tk.tk()
root.title('测试用例')
根几何体('850x300')
root.configure(background='ivory3')
标签\u fill=tk.标签(根,宽=80,高=1,背景='ivory3')
标签填充网格(行=1,列span=3)
textw=scrolledtext.scrolledtext(根,宽度=18,高度=2)
textw.grid(列=0,行=2,sticky='nsew')
textw.tag_configure('tag-left',justify='left')
textw.config(背景为淡灰色,前景为绿色,
font='arial 60 bold',wrap='word',relief='sunken',bd=5)
def func_ex(计数=无):
全球btn
btn.configure(状态=tk.DISABLED)
如果计数不是无:

如果计数为什么不以面向对象的方式编码呢?效果相当不错。该按钮在功能运行时被禁用,并在功能完成后重新启用。但是,在功能完成后按下按钮不会产生任何效果。我希望它再次调用函数。这可能是因为计数从未重置为0,所以它总是大于31。我将在上面的代码中修复此问题。