Tkinter 文本框组件 |
您所在的位置:网站首页 › tkinter的entry › Tkinter 文本框组件 |
import tkinter as tk
# from tkinter import messagebox
window = tk.Tk()
window.title('这是一个登录窗口')
window.geometry('300x450')
def print_data():
entry_user.delete(0, 100)
entry_pwd.delete(0, 100)
return '用户名是:%s, 密码是: %s' % (entry_user.get(), entry_pwd.get())
msg = '欢迎进入系统...'
image = tk.PhotoImage(file=r'F:\git\test-python\tkinterGui\tkinterImage\002.gif')
logo = tk.Label(window, image=image, text=msg, compound='bottom')
logo.grid(row=0, column=0, columnspan=2, pady=10, padx=10)
# 定义一个用户栏标签
user_name = tk.Label(window, text='用户名 : ')
user_name.grid(row=1)
# 定义一个密码栏标签
password = tk.Label(window, text='密 码 : ')
password.grid(row=2)
# 变量跟踪作用
user_ = tk.StringVar()
pwd_ = tk.StringVar()
# 设置两个文本框, grid是设置页面位置
entry_user = tk.Entry(window, show='', bg='white', highlightcolor='red')
entry_user.insert(0, '你好,我是插入的默认值')
entry_user.grid(row=1, column=1, pady=10)
entry_pwd = tk.Entry(window, show='*', highlightcolor='red', bg='white')
entry_pwd.insert(0, '******')
entry_pwd.grid(row=2, column=1, pady=10)
print_button = tk.Button(window, text='登 录', command=print_data)
print_button.grid(row=3, column=0, sticky='w', pady=10, padx=35)
quit_button = tk.Button(window, text='退 出', command=window.quit)
quit_button.grid(row=3, column=1, sticky='w', pady=10)
tk.mainloop() 控制台显示出来了我们输入的数据信息 F:\virtualEnvironment\venv\Scripts\python.exe F:/git/test-python/tkinterGui/tkinterExampleEntryOne.py 用户名是:我是你, 密码是: 999999 Process finished with exit code 0Entry中的get()方法,用于获取文本框中字符串内容 # 源码部分 def get(self): """Return the text.""" return self.tk.call(self._w, 'get')# 上述代码中,def print_data(): 函数运用get来从文本框中获取数据信息 return '用户名是:%s, 密码是: %s' % (entry_user.get(), entry_pwd.get())Entry中的insert()方法,用于创建默认值,Entry文本框建立好了再使用 # 源码部分 # index是指字符串插入的位置、string是我们要插入的字符串 def insert(self, index, string): """Insert STRING at INDEX.""" self.tk.call(self._w, 'insert', index, string)# 上述代码中,运用insert往文本框中插入数据 entry_user.insert(0, '你好,我是插入的默认值')Entry中的delete()方法,删除Entry文本框中的数据 # 源码部分 # first代表第一值,last代表最后一个值,默认值为None def delete(self, first, last=None): """Delete text from FIRST to LAST (not included).""" self.tk.call(self._w, 'delete', first, last)查询网上资料,好多吧last值写成这样 entry_user.delete(0, END) 实操后报错误:name 'END' is not defined Exception in Tkinter callback Traceback (most recent call last): File "D:\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "F:/git/test-python/tkinterGui/tkinterExampleEntryOne.py", line 18, in print_data entry_user.delete(0, END) NameError: name 'END' is not defined你们可以再研究下,我是搞不来了..... 运用了delete()方法,清空了默认值,因为是绑定按钮事件,必须点击下才能清除数据 。 未完,待续... 一直都在努力,希望您也是! |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |