简单tkinter的输入框应用——Entry

本文介绍了Python的tkinter库如何创建自定义窗口,包括设置窗口标题和图标,以及利用lambda表达式延迟Button事件的触发,展示了简单的GUI编程技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python自带的tkinter可以自定义,并且可以把事件封装好

#!/usr/bin/python
#-*-coding:utf-8-*-
import tkinter as tk
from tkinter import messagebox

#自定义tkinter类
class my_gui(tk.Frame):
    def __init__(self,parent=None):
        tk.Frame.__init__(self,parent)
        button = tk.Button(self,text='press',command=self.replay)
        button.pack()

    def replay(self):
        messagebox.showinfo(title='hello',message='Button Pressed!')
# def replay():
#     messagebox.showinfo(title='hello',message='Button Pressed!')
# window = tk.Tk()
# b1 = tk.Button(window,text='Press',command=replay)
# b1.pack()
# # tk.Label(text='Spam').pack()
# tk.mainloop()

class Customgui(my_gui):
    def replay(self):
        messagebox.showinfo(title='hahahah', message='From mygui')

if __name__ == '__main__':
    mainwindow =tk.Tk()
    tk.Label(mainwindow,text=__name__).pack()
    popup = tk.Toplevel()
    tk.Label(popup,text='Attach').pack(side='left')
    Customgui(popup).pack(side='right')
    # my_gui(popup).pack(side='right')
    # window = my_gui()   #生成自定义的Frame,继承tk.Frame
    # window.pack()
    # window.mainloop()
    mainwindow.mainloop()

修改窗口的标题及图标,以及使用lambda来延时button的事件。

#!/usr/bin/python
#-*-coding:utf-8-*-

import tkinter as tk
from tkinter import messagebox

def replay(name):
    messagebox.showinfo(title='Replay',message='Hello! ' + '%s'% name)

top = tk.Tk()
#修改标题
top.title('Echo')
#修改左上角图标
top.iconbitmap('../images/my_gui.ico')

tk.Label(top,text='Enter your name:').pack(side='top')
#使用Entry获取用户输入信息
ent = tk.Entry(top)
ent.pack()
#增加lambda来延时对replay函数的调用,使用get方法可以获取输入框的信息(默认是字符串)
btn = tk.Button(top,text='Submit',command=(lambda: replay(ent.get())))
btn.pack(side='left')

top.mainloop()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值