0

So I have a program that takes the most recent outlook e-mail and displays it once a button is pressed. What I want to do is get rid of the button and have the answer_label automatically run the timer function to display the e-mail at all times. Any suggestions?

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()
Prox
  • 699
  • 5
  • 11
  • 33
  • Don't have label run the function, have the button run it once but have the timer built into the function, so it'll run until something stops it aka a conditional statement. – Andria Apr 06 '17 at 19:16
  • That's actually the way it works now. But I was wanting to get rid of the button. I just want the emails to autostart once the program is opened up. – Prox Apr 06 '17 at 19:20
  • Then you could have the button set a variable that tells another part of your program to run the function and delete the button. – Andria Apr 06 '17 at 19:23
  • Or check out this: http://stackoverflow.com/questions/26601611/disable-tkinter-button-while-executing-command – Andria Apr 06 '17 at 19:24
  • 1
    In your mind, what is the difference between "label automatically run the timer function" and "run the timer function"? What does this have to do with a label? Why can't you simply call the function when you create the label? – Bryan Oakley Apr 06 '17 at 19:41
  • Sorry If I'm not very clear. Yeah, I just want the function to be called at the start of the program. The function provides e-mail text that is placed in the label. There's no button needed. I don't need to turn it on or off. I just want it on at startup. – Prox Apr 06 '17 at 20:15

1 Answers1

0

You do not need a button explicitly to run your timer function. Just call it within init. This piece of code works for me (it displays time instead of email).

#!/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()

Be careful with using tkinter in multi-threaded applications.

Booo
  • 493
  • 3
  • 13
  • Thank you. What kind of issues does multi-threading cause with Tkinter? – Prox Apr 07 '17 at 03:41
  • @Prox: At least in your code, the main thread is not in the main loop, which should be avoided. Here are some useful information: [Link1](http://stackoverflow.com/a/25351697/5520012) , [Link2](http://stackoverflow.com/a/14695007/5520012) . – Booo Apr 07 '17 at 15:03