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

如何从Tkinter Entry组件获取文本存入变量?WhatsApp群发程序求助

Fixing Your Tkinter Entry .get() Issue & WhatsApp Bulk Sender

Hey there! Let's get your WhatsApp message sender working properly—your core idea is great, but there are a few common Tkinter and scope issues that are making the .get() method fail. Let's break down the problems and fix them one by one.

Key Issues in Your Code

  • Entry Variable Assignment Mistake: When you write message = tkinter.Entry(window).grid(row=0, column=1), you're storing the return value of grid() (which is None) in message, not the actual Entry widget. That's why .get() doesn't work—you're calling it on None!
  • Variable Scope Problem: The message, recipient_one, etc. variables are local to your begin() function, so your message_send() function can't access them at all.
  • Multiple Tk Instances: Creating two separate Tk() windows (root and window) causes unexpected behavior. Use Toplevel() for child windows instead.
  • Wrong Button Parent: Your "Send" button is attached to the main root window instead of the input window you created in begin().
  • Invalid XPath: Your Selenium XPath is missing quotes around the title value, which will throw an error when trying to locate the chat.

Fixed Code

import tkinter as tk
import pyautogui
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

# Declare global variables to share between functions
message = None
recipient_one = None
recipient_two = None
recipient_three = None
recipient_four = None
recipient_five = None

def message_send():
    global message, recipient_one
    # Get values from Entry widgets
    target_one = recipient_one.get().strip()
    string = message.get().strip()
    
    if not target_one or not string:
        print("Please fill in both message and recipient fields!")
        return
    
    # Initialize Selenium driver
    driver = webdriver.Chrome('C:/Program Files(x86)/chromedriver_win32/chromedriver')
    driver.get("https://web.whatsapp.com/")
    wait = WebDriverWait(driver, 600)
    
    # Fixed XPath with proper quotes around title value
    x_arg = f'//span[contains(@title,"{target_one}")]'
    group_title = wait.until(EC.presence_of_element_located((By.XPATH, x_arg)))
    group_title.click()
    
    # Locate message input box and send message
    input_box = wait.until(EC.presence_of_element_located((By.XPATH, '//div[@contenteditable="true"][@data-tab="1"]')))
    input_box.send_keys(string + Keys.ENTER)

def begin():
    global message, recipient_one, recipient_two, recipient_three, recipient_four, recipient_five
    # Use Toplevel instead of Tk() for child window
    window = tk.Toplevel(root)
    window.title("Whatsapp Message Creator")
    
    # Create Entry widgets first, then grid them (so variables hold the widget, not None)
    tk.Label(window, text="Write advert here:").grid(row=0, column=0, padx=5, pady=5)
    message = tk.Entry(window, width=50)
    message.grid(row=0, column=1, padx=5, pady=5)
    
    tk.Label(window, text="Write recipient 1 here:").grid(row=1, column=0, padx=5, pady=5)
    recipient_one = tk.Entry(window, width=50)
    recipient_one.grid(row=1, column=1, padx=5, pady=5)
    
    tk.Label(window, text="Write recipient 2 here:").grid(row=2, column=0, padx=5, pady=5)
    recipient_two = tk.Entry(window, width=50)
    recipient_two.grid(row=2, column=1, padx=5, pady=5)
    
    tk.Label(window, text="Write recipient 3 here:").grid(row=3, column=0, padx=5, pady=5)
    recipient_three = tk.Entry(window, width=50)
    recipient_three.grid(row=3, column=1, padx=5, pady=5)
    
    tk.Label(window, text="Write recipient 4 here:").grid(row=4, column=0, padx=5, pady=5)
    recipient_four = tk.Entry(window, width=50)
    recipient_four.grid(row=4, column=1, padx=5, pady=5)
    
    tk.Label(window, text="Write recipient 5 here:").grid(row=5, column=0, padx=5, pady=5)
    recipient_five = tk.Entry(window, width=50)
    recipient_five.grid(row=5, column=1, padx=5, pady=5)
    
    # Attach send button to the child window
    send_button = tk.Button(window, text='Send', width=20, command=message_send)
    send_button.grid(row=6, column=0, columnspan=2, pady=10)

# Main window setup
root = tk.Tk()
root.title("WhatsApp Message Creator")

tk.Label(root, text="""Welcome to the Whatsapp Message Creator!""", 
         justify=tk.LEFT, padx=20, pady=10).pack()

begin_button = tk.Button(root, text='Begin', width=20, command=begin)
begin_button.pack(pady=5)

exit_button = tk.Button(root, text='Exit', width=20, command=root.destroy)
exit_button.pack(pady=5)

# Only one mainloop needed
root.mainloop()

What We Changed

  1. Entry Widget Fix: We now create each Entry widget first, then call .grid() separately. This ensures variables like message hold the actual Entry object, not None.
  2. Global Variables: We declared variables at the top and used the global keyword in functions to let them access and modify these shared variables.
  3. Child Window with Toplevel: Replaced the second Tk() instance with Toplevel(root) to create a proper child window attached to the main root window, avoiding Tkinter conflicts.
  4. Fixed Button Parent: The "Send" button is now part of the input window, not the main window.
  5. Valid XPath: Used an f-string to properly wrap the recipient name in quotes in the XPath, making it syntactically correct for Selenium.
  6. Added Input Validation: We check if the message and recipient fields are empty before proceeding, to avoid errors.
  7. Cleaned Imports & Mainloop: Removed duplicate imports and extra mainloop() calls—only one at the end is needed.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 12:18:12