You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IMAP未读消息计数程序无输出问题求助(含Tkinter界面)

Fixing Your Realtime Inbox Unread Counter in Tkinter

Hey there! Let's get your submission counter working properly—there are a couple of small but critical issues in your code that are causing the silent failure. Let's break them down:

1. The Label Isn't Being Added to the Window

You forgot parentheses when calling info.pack! Without them, the pack() method never runs, so your label never gets rendered on the window. That's why you see nothing in the UI.

2. Typo in Your Email Address

Your login line has a broken email format: 'email"gmail.com' should be 'email@gmail.com' (replace with your actual Gmail address, and fix the misplaced double quote to an @ symbol).

3. Gmail IMAP Login Requirements

If your Gmail account has 2-Step Verification enabled (which it should!), you can't use your regular password here. You need to generate an App Password from your Google Account settings. If you don't have 2-Step Verification on, you'll need to enable "Less secure app access" (though Google discourages this, so App Password is the better route).

4. Optional: Add Debug Output

To see what's happening in the command line, add a print() statement to your update() function—it'll help you verify if the unread count is being fetched correctly.


Fixed Code

Here's the corrected version with all these fixes applied:

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

WIDTH = 500
HEIGHT = 500

def update():
    try:
        # Connect to Gmail IMAP
        mail = imaplib.IMAP4_SSL('imap.gmail.com', 993)
        # Replace with your actual email and App Password (if 2FA is on)
        mail.login('your-email@gmail.com', 'your-app-password-or-password')
        mail.select("Submissions")
        
        # Search for unread messages
        typ, messageIDs = mail.search(None, "UNSEEN")
        messageIDsString = str(messageIDs[0], encoding='utf8')
        listOfSplitStrings = messageIDsString.split(" ")
        # Handle case where there are no unread messages (split gives empty string)
        number = len(listOfSplitStrings) if messageIDsString else 0
        
        # Update the label text
        if number == 0:
            info['text'] = 'No submissions'
        else:
            info['text'] = f'{number} submission{"s" if number !=1 else ""}'
        
        # Debug output to command line
        print(f"Current unread submissions: {number}")
        
        mail.close()
        mail.logout()
    except Exception as e:
        # Catch and print errors for debugging
        print(f"Error fetching data: {e}")
        info['text'] = 'Error fetching data'
    
    # Schedule next update (every 5 seconds)
    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(f'{WIDTH}x{HEIGHT}+{x}+{y}')

# Initialize label and properly pack it
info = tk.Label(root, text='Loading submissions...', font=('Arial', 16))
info.pack(pady=20)  # Added pady for better spacing

# Start the update loop
update()

root.mainloop()

Key Improvements:

  • Fixed info.pack() with parentheses to render the label
  • Corrected email format and added notes about App Passwords
  • Added error handling to catch and display issues (like login failures)
  • Fixed the count logic to handle empty unread results properly
  • Added debug prints to the command line
  • Improved the label text formatting (pluralization for "submission")
  • Added better spacing with pady in pack()

Just replace your-email@gmail.com and your-app-password-or-password with your actual credentials, and this should work—you'll see the count update every 5 seconds in both the Tkinter window and the command line.

内容的提问来源于stack exchange,提问作者Smokeyparkin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:41:57