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

求助:Tkinter的Entry组件无法接受输入,求解决方案

解决Entry组件无法接受输入的问题

核心问题及修复方案

1. 模态对话框导致主窗口焦点异常

代码在创建主窗口后立刻弹出tkinter原生messagebox,关闭对话框后主窗口可能丢失焦点,导致Entry无法接收输入。替换为ttkbootstrap专属对话框组件即可解决:

# 替换原messagebox导入与调用
from ttkbootstrap.dialogs import Messagebox

if Messagebox.askyesno("First Time", "Is this app opened for the FIRST TIME?"):
    cursor.execute('DROP TABLE IF EXISTS Stock')
    cursor.execute(''' CREATE TABLE Stock
            (Date TEXT,
            Quality TEXT,
            Quantity INT,
            Invoice INT,
            Supplier TEXT);''')

2. 组件混用导致交互异常

代码中使用了tkinter原生Entry,但ttkbootstrap主题需要配合自身组件使用。移除tk.前缀,直接使用ttkbootstrap的Entry(已通过from ttkbootstrap import *导入):

# 示例:原tk.Entry改为Entry
DatEntry = Entry(AddDataFrame, width=50, textvariable=Date)

3. SQL语句语法错误(附带修复)

AddData函数的INSERT语句多了一个右括号,会导致保存数据时报错,修复如下:

def AddData(Date, Quality, Quantity, Invoice, Supplier):
    cursor.execute(f'''INSERT INTO Stock (Date, Quality, Quantity, Invoice, Supplier) VALUES ('{Date}','{Quality}',{Quantity},{Invoice},'{Supplier}');''')
    conn.commit()  # 新增事务提交,确保数据写入数据库

4. 窗口生命周期管理适配

替换tk.mainloop()为ttkbootstrap窗口的frame.mainloop(),确保组件交互正常:

frame.mainloop()

完整修复代码

import tkinter as tk
from ttkbootstrap import *
from ttkbootstrap.dialogs import Messagebox
import sqlite3 as sql

conn = sql.connect("Stock Data.db")
cursor = conn.cursor()

def AddData(Date, Quality, Quantity, Invoice, Supplier):
    cursor.execute(f'''INSERT INTO Stock (Date, Quality, Quantity, Invoice, Supplier) VALUES ('{Date}','{Quality}',{Quantity},{Invoice},'{Supplier}');''')
    conn.commit()
def SortData():
    pass
def ResetAdd():
    Date.set("Date")
    Quality.set("Quality")
    Quantity.set("Quantity")
    Invoice.set("Invoice Number")
    Supplier.set("Supplier")
def ResetSort():
    pass

frame = Window("Stock Book", themename="superhero")

if Messagebox.askyesno("First Time", "Is this app opened for the FIRST TIME?"):
    cursor.execute('DROP TABLE IF EXISTS Stock')
    cursor.execute(''' CREATE TABLE Stock
            (Date TEXT,
            Quality TEXT,
            Quantity INT,
            Invoice INT,
            Supplier TEXT);''')

root = Frame(frame, padding=10)
root.pack(pady=5, fill=X, side=TOP)

nb = Notebook(root)
nb.pack(side=LEFT, padx=(10, 0), expand=YES, fill=BOTH)

AddDataFrame = Frame(nb, padding=15)
AddDataFrame.pack(fill=BOTH, pady=(10, 5), expand=YES)
Date = StringVar(AddDataFrame)  
DatEntry = Entry(AddDataFrame, width=50, textvariable=Date)
DatEntry.pack(fill=X, pady=5)
Quality = StringVar(AddDataFrame)
QualityEntry = Entry(AddDataFrame, textvariable=Quality)
QualityEntry.pack(fill=X, pady=5)
Quantity = StringVar(AddDataFrame)
QuantityEntry = Entry(AddDataFrame, textvariable=Quantity)
QuantityEntry.pack(fill=X, pady=5)
Invoice = StringVar(AddDataFrame)
InvoiceNumberEntry = Entry(AddDataFrame, textvariable=Invoice)
InvoiceNumberEntry.pack(fill=X, pady=5)
Supplier = StringVar(AddDataFrame)
SupplierEntry = Entry(AddDataFrame, textvariable=Supplier)
SupplierEntry.pack(fill=X, pady=5)
ResetButton = Button(AddDataFrame, text="RESET", style="Danger.TButton", width=22, command=ResetAdd)
ResetButton.pack(pady=10, side = LEFT)
SaveButton = Button(AddDataFrame, text="ADD DATA", style="Success.TButton", width=22, command=lambda:AddData(
    Date.get(),
    Quality.get(),
    Quantity.get(),
    Invoice.get(),
    Supplier.get()
))
SaveButton.pack(pady=10, side = RIGHT)

SortDataFrame = Frame(nb, padding=15)
SortDataFrame.pack(fill=BOTH, pady=(10, 5), expand=YES)
SorterEntry = Entry(SortDataFrame, width=50)
SorterEntry.pack(fill=X, pady=5)
SorterEntry.insert(END, "Sorter")
SortererEntry = Entry(SortDataFrame)
SortererEntry.pack(fill=X, pady=5)
SortererEntry.insert(END, "Value")
ResetButton = Button(SortDataFrame, text="RESET", style="Danger.TButton", width=22, command=ResetSort)
ResetButton.pack(pady=10, side = LEFT)
SortButton = Button(SortDataFrame, text="SORT", style="Success.TButton", width=22, command=SortData)
SortButton.pack(pady=10, side = RIGHT)

nb.add(AddDataFrame, text="Add Data", sticky=NW)
nb.add(SortDataFrame, text="Sort Data", sticky=NW)

frame.mainloop()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 15:47:36