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?

你可能感兴趣的:(Python)