如何在程序的文本窗口中实时更改外部接收的数据输出? [英] How to change the data output received externally in realtime within a program's text window?

查看:15
本文介绍了如何在程序的文本窗口中实时更改外部接收的数据输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直不愿意问这个问题 - 我一直在努力解决它并且到处寻找!

I have been reluctant to ask this question - I have been trying to solve it and have looked everywhere!

所以,我经营一家公司,我们有几个第三方通过电子邮件向我们发送销售通知 - 全部发送到不同的地址.发送给我们的每封电子邮件都是一次销售.问题是办公室的任何地方都无法看到那一刻的当前销售总额(提交).我从小就一直渴望学习 Python,所以我想我会找到一种很酷的方式来以图形方式显示一种在屏幕上有一个大数字的股票代码,我们可以挂在办公室的墙上让每个人都看到, 通过 Raspberry Pi 和 LCD.但我的天真这么难做.

So, I run a company where we have several third parties emailing us with notifications of sales - all to different addresses. Each email to us is exactly one sale. The issue is that nowhere in the office is it possible to see the current total of sales (submissions) for that moment. I have been dying to learn Python since I was young so I figured that I would find a cool way to graphically display a sort of ticker that has a big number on a screen that we can hang on a wall in the office for everyone to see, via a Raspberry Pi and an LCD. But my gawd is this tough to do.

在 Python 之外,一切都很好,我已经找到了将电子邮件发送到一个收件箱的方法,并且我已经成功地在我们的 Gmail 上设置了一个计时器来标记 24 小时后的阅读.我遇到的问题是,我真的希望程序能够自动实时更新未读计数,而不是永远向下滚动屏幕.

Outside of Python, it's all good to go, I have figured out the emailing to one inbox and I have managed to set a timer on our Gmail to mark the reads after 24hrs. The problem I am having is that I really want the program to update its counting of the unreads in real-time automatically without scrolling forever down the screen.

我到处寻找,循环似乎没有刷新窗口上的数字,除了再次重新启动程序之外没有任何作用,这不是自动的并且需要我自己再次启动程序.所以,,对此有任何帮助都会很棒.我打算稍后再添加图形,也许会有一些声音在打折时发出 ping 或 CHaaaChing 的声音,但我想我会先解决这些困难的问题.

I have looked everywhere, loops don't seem to refresh the number on the window, nothing works apart from restarting the program again which is not automated and requires myself to start the program again. So, please, any help on this would be great. I plan to add graphics later, and maybe a little sound that goes ping or CHaaaChing when a sale comes in, but I thought I'd get this hard stuff out of the way first.

这是我的代码:

#! /usr/bin/env python3.4
import imaplib
import email

from tkinter import *
root = Tk()


def window(main):
    main.title('submissions counter')
    width = 500
    height = 500
    x = (main.winfo_screenwidth() //2 ) - (width // 2)
    y = (main.winfo_screenheight() //2 ) - (height // 2)
    main.geometry('{}x{}+{}+{}'.format(width, height, x, y))

mail=imaplib.IMAP4_SSL('imap.gmail.com',993)
mail.login('email@gmail.com','Password')
mail.select("Submissions")
typ, messageIDs = mail.search(None, "UNSEEN")
messageIDsString = str( messageIDs[0], encoding='utf8' )
listOfSplitStrings = messageIDsString.split(" ")

if len(listOfSplitStrings) == 0:
    print('no submissions')
elif len(listOfSplitStrings) == 1:
    print(len(listOfSplitStrings),'submission[s]')
else:
    print(len(listOfSplitStrings),'submission[s]')

def main_content():
    hello = Label(root, text=(len(listOfSplitStrings),'submission[s]'))
    hello.pack()

window(root)
main_content()
root.mainloop()

现在我正在尝试使用此代码,但根本没有得到任何结果,没有错误消息,文本框或命令行上没有结果......

#! /usr/bin/env python3.4
import imaplib
import email
import tkinter as tk

WIDTH = 500
HEIGHT = 500


def update():
    mail=imaplib.IMAP4_SSL('imap.gmail.com',993)
    mail.login('email"gmail.com','password')
    mail.select("Submissions")
    typ, messageIDs = mail.search(None, "UNSEEN")
    messageIDsString = str( messageIDs[0], encoding='utf8' )
    listOfSplitStrings = messageIDsString.split(" ")

    number = len(listOfSplitStrings)

    if number == 0:
        info['text'] = 'no submissions'
    else:
        info['text'] = '{} submissions[s]'.format(number)

    root.after(5000, update)

root = tk.Tk()

root.title('submissions counter')

x = (root.winfo_screenwidth()//2) - (WIDTH//2)
y = (root.winfo_screenheight()//2) - (HEIGHT//2)
root.geometry('{}x{}+{}+{}'.format(WIDTH, HEIGHT, x, y))

info = tk.Label(root, text='no submissions')
info.pack

update()
root.mainloop()

推荐答案

看来你需要 root.after(milliseconds, function_name) 来周期性地执行函数而不是阻塞 mainloop() 在窗口中重绘小部件.

It seems you need root.after(milliseconds, function_name) to execute function periodically and not block mainloop() which redraws widgets in window.

我可以看起来像这样

#! /usr/bin/env python3.4
import imaplib
import email
import tkinter as tk
#import random # for test without using mail

# --- constants --- (UPPER_CASE_NAMES)

WIDTH = 500
HEIGHT = 500

# --- functions --- (lower_case_names)

def update():

    mail = imaplib.IMAP4_SSL('imap.gmail.com', 993)
    mail.login('email@gmail.com', 'Password')
    mail.select("Submissions")
    typ, messageIDs = mail.search(None, "UNSEEN")
    messageIDsString = str(messageIDs[0], encoding='utf8')
    listOfSplitStrings = messageIDsString.split(" ")

    number = len(listOfSplitStrings)    
    #number = random.randint(0, 5) # for test without using mail

    if number == 0:
        #print('no submissions')
        info['text'] = 'no submissions'
    else:
        #print(number, 'submission[s]')
        info['text'] = '{} submission[s]'.format(number)

    # update again after 5000ms (5seconds)
    root.after(5000, update)

# --- main --- (lower_case_names)

root = tk.Tk()

root.title('submissions counter')

x = (root.winfo_screenwidth()//2) - (WIDTH//2)
y = (root.winfo_screenheight()//2) - (HEIGHT//2)
root.geometry('{}x{}+{}+{}'.format(WIDTH, HEIGHT, x, y))

# create label for information
info = tk.Label(root, text='no submissions')
info.pack()

# update label first time
update()

root.mainloop()

这篇关于如何在程序的文本窗口中实时更改外部接收的数据输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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