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
在PythonTkinter中,如何使窗口处于空闲状态,直到按下按钮为止?_Python_Python 3.x_User Interface_Tkinter - Fatal编程技术网

在PythonTkinter中,如何使窗口处于空闲状态,直到按下按钮为止?

在PythonTkinter中,如何使窗口处于空闲状态,直到按下按钮为止?,python,python-3.x,user-interface,tkinter,Python,Python 3.x,User Interface,Tkinter,我有一个带有开始按钮的窗口,在不同的框架中设置了一些小部件。现在我想使窗口处于空闲状态(禁用状态),直到单击“开始”按钮。请帮助我解决此问题。我的代码如下所示 from tkinter import * class Application(Frame): def __init__(self, master): super(Application,self).__init__(master) self.grid() self.create_widgets()

我有一个带有开始按钮的窗口,在不同的框架中设置了一些小部件。现在我想使窗口处于空闲状态(禁用状态),直到单击“开始”按钮。请帮助我解决此问题。我的代码如下所示

from tkinter import *



class Application(Frame):

def __init__(self,  master):
    super(Application,self).__init__(master)
    self.grid()
    self.create_widgets()

  def create_widgets(self):
    self.be=Button(self,text="start")
    self.be.grid(row=1,column=1,sticky="s")

    self.columnconfigure(0, pad=3)
    self.columnconfigure(1, pad=3)
    self.columnconfigure(2, pad=3)
    self.columnconfigure(3, pad=3)

    self.rowconfigure(0, pad=5)
    self.rowconfigure(1, pad=5)
    self.f3=Frame(self)
    self.f3.grid(row=2,column=1)
    fuel = Button(self.f3, text="Button",width=19)
    fuel.grid(row=1, column=0)
    Temp = Button(self.f3, text="Button",width=19)
    Temp.grid(row=1, column=1)
    Th = Button(self.f3,text="Button",width=19)
    Th.grid(row=1, column=2)    
    Eo = Button(self.f3, text="Button",width=19)
    Eo.grid(row=1, column=3)

    Bh = Button(self.f3, text="Button",width=19)
    Bh.grid(row=2, column=0)        
    Abs = Button(self.f3, text="Button",width=19)
    Abs.grid(row=2, column=1)         
    sp = Button(self.f3, text="Button",width=19)
    sp.grid(row=2, column=2) 
    fo = Button(self.f3, text="Button",width=19)
    fo.grid(row=2, column=3) 

root= Tk()
app=Application(root)
app.mainloop()

您更需要在小部件上循环并逐个禁用它们,因为您不能像这样禁用整个
框架
对象

因此,您需要使用来探测子窗口小部件

然后,您需要使用回调将一个方法附加到
self.be
按钮,以再次启用子小部件

完整程序 以下是您正在寻找的程序:

'''
Created on Apr 29, 2016

@author: billal begueradj
'''
from tkinter import *

class Application(Frame):

   def __init__(self,  master):
       super(Application,self).__init__(master)
       self.grid()
       self.create_widgets()

   def create_widgets(self):
       self.be=Button(self,text="start", command = self.enable_widgets)
       self.be.grid(row=1,column=1,sticky="s")

       self.columnconfigure(0, pad=3)
       self.columnconfigure(1, pad=3)
       self.columnconfigure(2, pad=3)
       self.columnconfigure(3, pad=3)

       self.rowconfigure(0, pad=5)
       self.rowconfigure(1, pad=5)
       self.f3=Frame(self)
       self.f3.grid(row=2,column=1)
       fuel = Button(self.f3, text="Button",width=19)
       fuel.grid(row=1, column=0)
       Temp = Button(self.f3, text="Button",width=19)
       Temp.grid(row=1, column=1)
       Th = Button(self.f3,text="Button",width=19)
       Th.grid(row=1, column=2)    
       Eo = Button(self.f3, text="Button",width=19)
       Eo.grid(row=1, column=3)

       Bh = Button(self.f3, text="Button",width=19)
       Bh.grid(row=2, column=0)        
       Abs = Button(self.f3, text="Button",width=19)
       Abs.grid(row=2, column=1)         
       sp = Button(self.f3, text="Button",width=19)
       sp.grid(row=2, column=2) 
       fo = Button(self.f3, text="Button",width=19)
       fo.grid(row=2, column=3) 
       for child in self.f3.winfo_children():
           child.configure(state = 'disable')


   def enable_widgets(self):
       '''Enable the child widgets'''
       for child in self.f3.winfo_children():
           child.configure(state = 'normal')


root= Tk()
app=Application(root)
app.mainloop()
演示 在程序开始时,GUI将如下所示:

单击
开始
按钮后:


我还有一个查询。在上面的程序中,每个按钮执行单独的功能。单击按钮时,新窗口将打开,这是一个子类。现在我想同时执行每个按钮的功能。例如,如果我单击按钮1,即使我点击按钮2,它也应该继续接受文件的输入并给出输出。我可以在这里使用线程概念吗?如果可以,你能告诉我怎么做吗?在这样的事情上使用线程是没有用的。只需使用option命令将这些回调方法附加到按钮,但如果我使用两个按钮,这两个函数都将无法工作simultaneously@Billalbut如何同时单击两个按钮?这是不可能的:)这是真的,但例如,如果我点击按钮1,它收集来自某个传感器的读数并显示在gui上,现在如果我点击按钮2,它做同样的事情。当我点击第二个按钮时,按钮1的结果也应继续显示结果,而第二个按钮的结果也应显示出来直到两者都被用户终止。