python用tkinter对输入的字符处理后输出到文本框中
February 25, 2024
Tkinter, Python, 文本, 字符

python用tkinter对输入的字符处理后输出到文本框中

February 25, 2024
Tkinter, Python, 文本, 字符
import tkinter as tk
from tkinter import messagebox
stoped=False 
def run_program():
    # 获取输入框内容
    input_text = entry.get()
    
    if not input_text:
        messagebox.showinfo("提示", "请先输入内容")
    else:
        # 这里写需要运行的程序代码
        print(f"输入的字符为{input_text}")
        textarea.insert('end', f"{input_text}\n") # 将输入的内容添加到文本框中
        # 清空输入框内容
        entry.delete(0, 'end')
def stop_button():
    global stoped
    if not stoped:
        # 如果未处于停止状态就停止
        stoped = True
        exit_button.config(text="停止")
        if messagebox.askyesno("确认", "你确定要退出程序吗?"):
            window.destroy()#关闭面板,否则程序退出面板未关闭而卡住 
# 创建主窗口
window = tk.Tk()
window.title('输入框与按钮示例')
window.geometry("600x300")
 
# 创建输入框
entry = tk.Entry(window)
entry.pack()
entry.place(x=10, y=10)  
# 创建确认按钮
confirm_button = tk.Button(window, text='确定', command=run_program)
confirm_button.pack()
confirm_button.place(x=10, y=40) 
# 创建退出按钮
exit_button = tk.Button(window, text='退出', command=stop_button)
exit_button.pack()
exit_button.place(x=60, y=40)  
textarea = tk.Text(window)
textarea.pack() 
textarea.place(x=10, y=80)  
# 进入消息循环,mainloop函数是Python Tkinter库中的一个函数,它的作用是开启一个事件循环,
#用于监听用户的操作。当用户进行某些操作时,比如点击按钮、输入文本等,mainloop函数会监听这些操作,
#并根据用户的操作执行相应的函数。
#例如,在一个GUI程序中,用户点击了一个按钮,mainloop函数会检测到这个事件,并执行与按钮相应的函数。
window.mainloop()

© 2024 Powered By Hugo,模板:Hugo-book . Contact: tndyx@hotmail.com

本站总访问量 本文总阅读量