tkinter程序GUI行为打开两个窗口而不是一个,标签未更新

2024-10-03 13:27:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下程序,用于创建非常基本的GUI:

from __future__ import with_statement
from Tkinter import *
import tkFont
import test

class main_fun():
    def init_proc(self):
        stLabelStatus['text']="Started executing the test...."
        time.sleep(5)

    def procA(self):
        print "inside ProcA...."
        stLabelStatus['text'] = "Running ProcA...."
        time.sleep(5)

    def procB(self):
        print "inside ProcB...."
        stLabelStatus['text'] = "Running ProcB...."
        time.sleep(5)

    def procC(self):
        print "inside ProcC...."
        stLabelStatus['text'] = "Running ProcC...."
        time.sleep(5)

    def procD(self):
        print "inside ProcD...."
        stLabelStatus['text'] = "Running ProcD...."
        time.sleep(5)

    def procE(self):
        print "inside ProcE...."
        stLabelStatus['text'] = "Running ProcE...."
        time.sleep(5)

    def procF(self):
        print "inside ProcF...."
        stLabelStatus['text'] = "Running ProcF...."
        time.sleep(5)

    def main_control(self):
        stLabelStatus.config(text='Running Tests....')
        self.init_proc()
        self.procA()
        self.procB()
        self.procC()
        self.procD()
        self.procE()
        self.procF()

tests=main_fun()

stGUI = Tk()
stGUI.geometry("450x300+50+50")
stGUI.resizable(0,0)
stGUI.title("Testing GUI")

arial12 = tkFont.Font(root = stGUI, family='Arial', size=12, weight='bold')
arial16 = tkFont.Font(root = stGUI, family='Arial', size=16, weight='bold')

stLabelTestNumlbl = Label(stGUI,text="Test No.")
stLabelTestNumlbl['font'] = arial12
stLabelTestNumlbl.place(x=120,y=40)

stInputTestNum = Entry(stGUI, width=6)
stInputTestNum['font'] = arial12
stInputTestNum.place(x=250,y=40)

stLabelStatuslbl = Label(stGUI,text="Status")
stLabelStatuslbl['font'] = arial12
# stLabelStatus.pack()
# stLabelStatus.grid(row=0,column=0)
stLabelStatuslbl.place(x=20,y=180)

stLabelResultlbl = Label(stGUI,text="Result")
stLabelResultlbl['font'] = arial12
stLabelResultlbl.place(x=20,y=240)

stButtonStart = Button(stGUI,text="Start", command=tests.main_control)
stButtonStart['font'] = arial16
stButtonStart.place(x=180,y=110)

stLabelStatus = Label(stGUI, text="", anchor=CENTER)
stLabelResult = Label(stGUI, text="", anchor=CENTER)
stLabelStatus.place(x=90,y=183)
stLabelResult.place(x=90,y=243)

stGUI.mainloop()

尽管GUI是创建的,但这里有几个问题:

  1. 有一个名为tk的小窗口,它与我的名为Testing GUI的窗口一起打开(见下图)。我记得我刚开始的时候它不在那里,看起来像是我最近添加的东西创造的。我真的不明白到底是什么。我怎样才能移开那扇小小的虫子窗,或者至少把它藏起来?你知道吗

Windows

  1. 即使在流中插入了stLabelStatus['text']="Running ProcA...."和类似的语句,我也无法在运行时更新状态标签。我只能看到流中的最后一行(procF是最后调用的函数)stLabelStatus['text']="Running ProcF...."。你知道吗

如何根据我的要求在运行时更新状态?我确信这是可能的,我犯了一些合乎逻辑的错误。你知道吗


Tags: textselftimedefplaceguisleeprunning
1条回答
网友
1楼 · 发布于 2024-10-03 13:27:14

抱歉,我错过了正确引用tests对象(您的自定义类)的命令。你知道吗

但是这个

#!python3

from tkinter import *

class main_fun():
    def main_c():
        print("main_c")
        print(stLabelStatus)
        stLabelStatus['text']="Status updated...."

tests=main_fun()

stGUI = Tk()
stGUI.geometry("450x300+50+50")
stGUI.resizable(0,0)
stGUI.title("Testing GUI")

stButtonStart = Button(stGUI,text="Start", command=tests.main_c)
stButtonStart.place(x=180,y=100)

stLabelStatus = Label(stGUI, text="", anchor=CENTER)
stLabelStatus.place(x=90,y=183)

stGUI.mainloop()

提供以下输出:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python35\lib\tkinter\__init__.py", line 1550, in __call__
    return self.func(*args)
TypeError: main_c() takes 0 positional arguments but 1 was given

这说明了一个确切的问题,可能会在网络搜索结果中给出答案。你知道吗

但是通过对参数的提示,我们会注意到类定义及其唯一的方法。你会看到Python documentation or tutorial on classes并注意到你做错了:

class没有参数,但方法必须至少有一个(self):

class main_fun:
    def main_c(self):
        print("main_c")
        print(stLabelStatus)
        stLabelStatus['text']="Status updated...."

成功!你知道吗


使用全新的Python 2.7.13进行测试:

#!python2
#coding=utf-8
from Tkinter import *

def my_method():
    stLabelStatus['text']="Status updated...."

stGUI = Tk()

stButtonStart = Button(
    stGUI,
    text="Start",
    command=my_method
    )
stButtonStart.place(x=0, y=0)

stLabelStatus = Label(stGUI)
stLabelStatus.place(x=0, y=30)

stGUI.mainloop()

相关问题 更多 >