本篇介绍一下几个知识点:
(1)Label与Button
(2)Entry与Text
(1)Label与Button
import tkinter as tk
window = tk.Tk() #对象,object
window.title('my window')
window.geometry('500x300') #geometry译为几何。
var = tk.StringVar() #是tkinter里面类似字符串的类型
l = tk.Label(window,textvariable = var,bg = 'green',font = ('Arial',12),width = 15,height = 2)
l.pack() #可以用pack放置,也可以明确一个坐标放置
on_hit = False
def hit_me():
global on_hit
if on_hit == False:
on_hit = True
var.set('you hit me')
else:
on_hit = False
var.set('')
b = tk.Button(window,text = 'hit me',width = 15,height = 2,command = hit_me)
b.pack()
window.mainloop()# 更新窗口数据
自己编写的程序,业余练习01
import tkinter as tk
pingcha = tk.Tk()
pingcha.title('北斗导航系统')
pingcha.geometry('700x500')
#设置两个字符串
var1 = tk.StringVar()
var2 = tk.StringVar()
#设置两个label
l = tk.Label(pingcha,textvariable = var1,bg = 'blue',font = ('Arial',15),width = 20,height = 3)
l.pack()
m = tk.Label(pingcha,textvariable = var2,bg = 'green',font = ('Arial',15),width = 20,height = 3)
m.pack()
#不仅可以放置字符串,也可以放置数字
def LvBoTuiGu():
a1 = 45
a2 = 98
a3 = a1-a2
a4 = a1+a2
if a1>a2:
var2.set(a3)
else:
var2.set(a4)
var1.set('近代平差理论')
anniu1 = tk.Button(pingcha,text = '最小二乘与滤波',width = 25,height = 3,command = LvBoTuiGu)
anniu1.pack()
(2)Entry与Text
import tkinter as tk
window = tk.Tk() #对象,object
window.title('my window')
window.geometry('500x300')
e = tk.Entry(window,show = '6')#show = '*'
e.pack()
def insert_point():
var = e.get()
t.insert('insert',var)
def insert_end():
var = e.get()
t.insert('end',var)#t.insert(1.1,var),第一行,第一位
b1 = tk.Button(window,text = 'insert point',width = 15,height = 2,command = insert_point)
b1.pack()
b2 = tk.Button(window,text = 'insert end',width = 15,height = 2,command = insert_end)
b2.pack()
t = tk.Text(window,height = 2)
t.pack()
window.mainloop()# 更新窗口数据
# entry text
自己编写的程序,业余练习02
import tkinter as tk
import string
w1 = tk.Tk()
w1.title('北斗导航系统')
w1.geometry('500x300')
e1 = tk.Entry(w1,show = '')
e1.pack()
e2 = tk.Entry(w1,show = '')
e2.pack()
def plus():
var1 = float(e1.get()) #接受两个数字,转换为数字型
var2 = float(e2.get())
var3 = var1-var2
var4 = var3*var1
t.insert('insert',var4) #将最终的计算结果,插入到t里面
b1 = tk.Button(w1,text = '最小二乘配置解',width = 15,height = 2,command = plus)
b1.pack()
t = tk.Text(w1,height = 2)
t.pack()
附加本次学习的所有原始代码:
## Tkinter
'''
import tkinter as tk
window = tk.Tk() #对象,object
window.title('my window')
window.geometry('500x300')
var = tk.StringVar()
l = tk.Label(window,textvariable = var,bg = 'green',font = ('Arial',12),width = 15,height = 2)
l.pack()
on_hit = False
def hit_me():
global on_hit
if on_hit == False:
on_hit = True
var.set('you hit me')
else:
on_hit = False
var.set('')
b = tk.Button(window,text = 'hit me',width = 15,height = 2,command = hit_me)
b.pack()
window.mainloop()# 更新窗口数据
'''
import tkinter as tk
window = tk.Tk() #对象,object
window.title('my window')
window.geometry('500x300')
e = tk.Entry(window,show = '6')#show = '*'
e.pack()
def insert_point():
var = e.get()
t.insert('insert',var)
def insert_end():
var = e.get()
t.insert('end',var)#t.insert(1.1,var),第一行,第一位
b1 = tk.Button(window,text = 'insert point',width = 15,height = 2,command = insert_point)
b1.pack()
b2 = tk.Button(window,text = 'insert end',width = 15,height = 2,command = insert_end)
b2.pack()
t = tk.Text(window,height = 2)
t.pack()
window.mainloop()# 更新窗口数据
# entry text