Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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,因此,我有一个程序,它接收最新的outlook电子邮件,并在按下按钮后显示它。我想做的是去掉按钮,让应答标签自动运行计时器功能,以便随时显示电子邮件。有什么建议吗 import win32com.client import os import threading # use the Timer import tkinter from tkinter import Tk, Label, Button class myGUI: def timer(self): imp

因此,我有一个程序,它接收最新的outlook电子邮件,并在按下按钮后显示它。我想做的是去掉按钮,让应答标签自动运行计时器功能,以便随时显示电子邮件。有什么建议吗

import win32com.client
import os
import threading # use the Timer
import tkinter

from tkinter import Tk, Label, Button

class myGUI:

    def timer(self):

        import pythoncom           # These 2 lines are here because COM library
        pythoncom.CoInitialize()   # is not initialized in the new thread

        outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

        inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
                                            # the inbox. You can change that number to reference
                                            # any other folder

        messages = inbox.Items
        message = messages.GetLast()
        body_content = message.Body


        self.answer_label['text'] = (body_content) # orginally used body_content.encode("utf-8") to fixed character encoding issue
                             # caused by Windows CMD.  But then figured out you can type 'chcp 65001' in cmd
        threading.Timer(5, self.timer).start() # refresh rate goes here


    def __init__(self, master):
        self.master = master
        master.title("CheckStat")


        self.answer_label = Label(master, text='')
        self.answer_label.place(height=300, width=300)


        self.greet_button = Button(master, text="Start", command=self.timer)
        self.greet_button.place(height=20, width=100)


    def greet(self):
        print("Greetings!")


root = Tk()
my_gui = myGUI(root)
root.mainloop()

您不需要显式地使用按钮来运行计时器功能。只需在init内调用它。这段代码适合我(它显示时间而不是电子邮件)


在多线程应用程序中使用tkinter时要小心。

不要让label运行函数,让按钮运行一次,但要在函数中内置计时器,这样它会一直运行,直到有什么东西停止它,也就是条件语句。这实际上是它现在的工作方式。但我想把这个按钮去掉。我只想在程序打开后自动启动电子邮件。然后你可以让按钮设置一个变量,告诉程序的另一部分运行该功能并删除该按钮。或者看看这个:在你看来,“标签自动运行计时器功能”和“运行计时器功能”有什么区别?这与标签有什么关系?为什么在创建标签时不能简单地调用函数呢?谢谢。Tkinter的多线程会导致什么问题?@Prox:至少在您的代码中,主线程不在主循环中,这应该避免。以下是一些有用的信息:。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import datetime
import threading # use the Timer
from tkinter import Tk, Label, Button

class myGUI:

    def timer(self):
        self.answer_label['text'] = datetime.datetime.now()
        threading.Timer(5, self.timer).start()

    def __init__(self, master):
        self.master = master
        master.title("CheckStat")

        self.answer_label = Label(master, text='')
        self.answer_label.place(height=300, width=300)

        self.timer()

root = Tk()
my_gui = myGUI(root)
root.mainloop()