在方法内部带有tkinter的Python显示变量 [英] Python display variable with tkinter inside a method

查看:120
本文介绍了在方法内部带有tkinter的Python显示变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚编写的程序是我了解Tkinter工作原理的游乐场.
我的问题是如何将变量"timelabel"显示为标签.

The program I just wrote is my playground for finding out how Tkinter works.
My question is how do I display my variable "timelabel" as a Label.

我已经制作了一个名为timerefresher的标签,但它没有显示.

I allready made a label called timerefresher yet it doesnt show up.

我知道DigitalClock类的编写效率不高.我是这个新手;).

I am aware that the DigitalClock class isn't written efficiently. I am new to this ;).

def Clockupdate(time):
    timelabel = time
    timerefresher = Label(root, textvariable=timelabel)
    timerefresher.pack()
    #print(timelabel) To test if variable made it this far.

class DigitalClock:
    def secondrefesher(self):
        newtime = ""
        while 1 == 1:
            oldtime = datetime.datetime.now()
            a = str(oldtime.hour)
            b = str(oldtime.minute)
            c = str(oldtime.second)
            curtime = (a+":"+b+':'+c)
            if curtime != newtime:
                newtime = curtime
                #print("made it here")
                Clockupdate(newtime)
            else:
                time.sleep(0.20)

DC = DigitalClock()

root = Tk()
mainlabel = Label(root, text="Hey this is working!")
Pressme = Button(root, text="Press me!", command=Presstoshowtextandpic, 
bg='red', fg='white')
Clock = Button(root, text="Clock?", command=DC.secondrefesher, bg='blue', 
fg='white')
Photo = PhotoImage(file="test.png")
ShowPhoto = Label(root, image=Photo)

mainlabel.pack()
Pressme.pack()
Clock.pack()
root.mainloop()

推荐答案

好的,这就是我将使用您的代码来完成您要完成的工作.

Ok so Here is what I would do with your code to accomplish what you are trying to do.

首先,请确保我们具有正确的导入.

First lets make sure we have the correct imports.

from tkinter import *
import datetime
import time

我创建了这个doNothing()函数,用作我当前未测试的任何命令的占位符.

I created this doNothing() function as a place holder for any commands I am not currently testing.

def doNothing():
    pass

现在,您使用Clockupdate(time):函数的方式将使您每次单击时钟按钮时都可以添加标签.因此,这可能不是您想要的,因为它只是不断添加新标签,而不是替换现有标签.看一下这个功能.我在其中所做的只是将.config()文本用作该函数的时间参数.请注意,您无需重新定义时间标签或其他任何类型的时间,只需将时间用作变量即可.

Now the way you had your Clockupdate(time): function would cause the label to be added every time you click the clock button. so this is probably not what you want as it just keeps adding new labels instead of replacing the existing one. Take a look at this function. All I do in this is .config() the text to be the time argument from the function. Note you do not need to redefine the time to timelabel or anything of the sort, just use time as your varable.

def Clockupdate(time):
    timerefresher.config(text = time)

我不知道您是否出于特定原因想要使用类,但我认为您应该在此处使用一个函数.也请尝试使您的报价保持一致.我知道它们是可以互换的,但是保持它们一致的优良作法.

I don't know if you wanted to use a class for a specific reason but I think you should just use a function here. Also try to keep your quotes consistent. I know they are interchangeable but its good practice to keep them consistent.

def secondrefesher():
    newtime = ""
    oldtime = datetime.datetime.now()
    a = str(oldtime.hour)
    b = str(oldtime.minute)
    c = str(oldtime.second)
    curtime = (a + ":" + b + ":" + c)
    if curtime != newtime:
        newtime = curtime
        Clockupdate(newtime)

注意,我总是将命令放在按钮的任何配置的末尾.它在管理代码时有帮助,因此您可以在滚动代码时检查每一行的末尾.我还将Pressme的命令更改为doNothing,以便可以测试代码.您没有提供Presstoshowtextandpic的代码,并且如果没有该部分就无法测试该代码.请记住,在此处提出问题以使用 MCVE .

Note I always place commands at the end of any config of a button. It helps when managing code so you can just check the end of each line as you scroll through your code. I also changed the command for Pressme to doNothing so I could test the code. You didn't provide the code for Presstoshowtextandpic and would not be able to test the code with out that part. Remember when asking a question here to use MCVE.

root = Tk()
mainlabel = Label(root, text="Hey this is working!")
Pressme = Button(root, text = "Press me!", bg = "red", fg = "white", command  = doNothing)
Clock = Button(root, text = "Clock?", bg = "blue", fg = "white", command = secondrefesher)
Photo = PhotoImage(file = "test.png")
ShowPhoto = Label(root, image = Photo)

在这里,我只创建了一次时间标签,然后您就可以调用全部要将文本更改为当前时间的更新功能.

Here I created the time label just once and then you can call the update function all you want to change the text to current time.

timerefresher = Label(root, text = "")
timerefresher.pack()

mainlabel.pack()
Pressme.pack()
Clock.pack()
root.mainloop()

这就是我认为完整的代码对您来说应该是什么样子.

Here is what I think the completed code should look like for you.

from tkinter import *
import datetime
import time

def Clockupdate(time):
    timerefresher.config(text = time)

def secondrefesher():
    newtime = ""
    oldtime = datetime.datetime.now()
    a = str(oldtime.hour)
    b = str(oldtime.minute)
    c = str(oldtime.second)
    curtime = (a + ":" + b + ":" + c)
    if curtime != newtime:
        newtime = curtime
        Clockupdate(newtime)

root = Tk()
mainlabel = Label(root, text = "Hey this is working!")
Pressme = Button(root, text = "Press me!", bg = "red", fg = "white", command  = Presstoshowtextandpic)
Clock = Button(root, text = "Clock?", bg = "blue", fg = "white", command = secondrefesher)
Photo = PhotoImage(file = "test.png")
ShowPhoto = Label(root, image = Photo)

timerefresher = Label(root, text = "")
timerefresher.pack()

mainlabel.pack()
Pressme.pack()
Clock.pack()
root.mainloop()

如果您尝试创建一个始终处于活动状态并且不需要单击按钮的时钟,则可以使用.after().

In case you are trying to create a clock that is always active and does not require you to click the button you can use .after().

看看这个例子.

from tkinter import *
import datetime
import time

root = Tk()
mainlabel = Label(root, text = "Hey this is working!")
Photo = PhotoImage(file = "test.png")

timerefresher = Label(root, text = "")
timerefresher.pack()

status_time = ""
def tick():
    global status_time
    time2 = time.strftime("%H:%M:%S")
    if time2 != status_time:
        status_time = time2
        timerefresher.config(text = time2)
    timerefresher.after(200, tick)
tick()

mainlabel.pack()
root.mainloop()

这篇关于在方法内部带有tkinter的Python显示变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆