Python界面设计Tkinter之简单实例

本篇是简单的实例,用到的理论知识在上一篇:Python界面GUI学习路程之Tkinter之窗口交互设计

声名:文中所用代码运行环境:Window 10 64bit,Python 3.8


目录

1.获取文本框Entry中的内容

2.限制文本框的输入内容

【参考文献】


1.获取文本框Entry中的内容

from tkinter import *
import tkinter as tk
from tkinter import ttk

def close_window():
    global entry
    entry = E.get()
    print("Entered text:", entry)
    root.destroy()

root = Tk()
E = tk.Entry(root)
E.pack(anchor = CENTER)
B = Button(root, text = "OK", command = close_window)
B.pack(anchor = S)
root.mainloop()
from tkinter import *
root=Tk()
def retrieve_input():
    inputValue=textBox.get("1.0","end-1c")
    #截取特定内容,1.0表示从第一行最开始的字符开始截取到最后一个,get运行完会自动添加一个字符,因此截取时最后减去一个字符,
    print(inputValue)

textBox=Text(root, height=2, width=10)
textBox.pack()
buttonCommit=Button(root, height=1, width=10, text="Commit",command=lambda: retrieve_input())
#command=lambda: retrieve_input() >>> just means do this when i press the button
buttonCommit.pack()

mainloop()
import tkinter as tk
from tkinter import ttk

root_window=tk.Tk()
root_window.title('Simulate poisson process ')
root_window.geometry('800x200')

def get_inventory():#按键的动作函数,为了获取用户输入的数据,一定要在引用前
    global inventory
    inventory = Inventory_entry.get()
    #root.destroy()
    
#获得相关数据 
tk.Label(root_window,text='Please input the related information.',bg='yellow',font=('Arial',12,'bold'),width=50,height=2).place(relx=0.2,y=30)

tk.Label(root_window,text='Inventory:',bg='yellow',font=('Arial',12,'bold'),width=10,height=1).place(relx=0.2,y=100)
Inventory_entry = tk.Entry(root_window,text="Inventory",show=None,width=30)
Inventory_entry.place(relx=0.4,y=100)
button = Button(root_window,text="Inventory",font=('Arial',12),width=10,height=1,command=get_inventory)
button.pack(side=tk.BOTTOM)
root_window.mainloop()

#可以通过prin语句验证
print(inventory)

2.限制文本框的输入内容

 

 

【参考文献】

1.Python Tkinter Entry get()

2.Entry Widgets

3.How to get the input from the Tkinter Text Widget?

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值