pythonGUI编程tkinter(莫烦教学项目小练习)(上卷)

Label&&Button

import tkinter as tk

window = tk.Tk()
window.geometry('300x150')
window.title('my first window')

var = tk.StringVar()  #字符串变量
label = tk.Label(window, textvariable=var, bg='green', font=('Arial',12), width=15, height=2)
label.pack()

on_hit = False
def hit_me():
    global on_hit
    if on_hit == False:
        var.set('You hit me')
        on_hit = True
    else:
        var.set('')
        on_hit = False

button = tk.Button(window, text='hit me', width=15, height=1, command=hit_me)
button.pack()

window.mainloop()

pythonGUI编程tkinter(莫烦教学项目小练习)(上卷)_第1张图片
Entry&&Text

import tkinter as tk

window = tk.Tk()
window.title("Entry&&Text")
window.geometry('300x250')

entry = tk.Entry(window, show=None)
entry.pack()
text = tk.Text(window, width=15,height=2)

def insert_point():
    var = entry.get()
    text.insert('insert',var)

def insert_end():
    var = entry.get()
    text.insert('end',var)

button_1 = tk.Button(window, text='insert point', width=15, height=2, command=insert_point)
button_1.pack()
button_2 = tk.Button(window, text='insert end', command=insert_end)
button_2.pack()

text.pack()

window.mainloop()

pythonGUI编程tkinter(莫烦教学项目小练习)(上卷)_第2张图片
ListBox

import tkinter as tk

window = tk.Tk()
window.title('ListBox')
window.geometry('300x350')

var1 = tk.StringVar()
label = tk.Label(window, bg='yellow', width=4, height=2, textvariable=var1)
label.pack()

def print_selection():
    value = listbox.get(listbox.curselection())
    var1.set(value)

button = tk.Button(window, text='print selection', command=print_selection)
button.pack()

var2 = tk.StringVar()
var2.set((11,22,33,44))
listbox = tk.Listbox(window,listvariable=var2)
listbox.pack()
listitems = [1,2,3,4]
for item in listitems:
    listbox.insert('end', item)
listbox.insert(1,'first')
listbox.insert(1,'second')
listbox.delete(1)

window.mainloop()

pythonGUI编程tkinter(莫烦教学项目小练习)(上卷)_第3张图片

RadioButton

import tkinter as tk

window = tk.Tk()
window.title('RadioBox')
window.geometry('300x250')

var = tk.StringVar()
label = tk.Label(window, bg='yellow', width=20, height=2, text='empty')
label.pack()

def print_selection():
    label.config(text='You have selected ' + var.get()) 

var.set('A')
r1 = tk.Radiobutton(window, text='Option A', variable=var, value='A', command=print_selection)
r1.pack()
r2 = tk.Radiobutton(window, text='Option B', variable=var, value='B', command=print_selection)
r2.pack()
r3 = tk.Radiobutton(window, text='Option C', variable=var, value='C', command=print_selection)
r3.pack()
r4 = tk.Radiobutton(window, text='Option D', variable=var, value='D', command=print_selection)
r4.pack()

window.mainloop()

pythonGUI编程tkinter(莫烦教学项目小练习)(上卷)_第4张图片

Scale

import tkinter as tk

window = tk.Tk()
window.title('Scale')
window.geometry('200x150')

label = tk.Label(window,bg='yellow',width=20,height=2,text='empty')
label.pack()

def print_selection(v):
    label.config(text="You have selected " + v)


scale = tk.Scale(window,label='try me',from_=5,to=11,orient=tk.HORIZONTAL,length=200,
showvalue=False,tickinterval=2,resolution=0.01,command=print_selection) #Scale调用函数时会默认传进参数,即Scale所指示的字

scale.pack()
window.mainloop()

pythonGUI编程tkinter(莫烦教学项目小练习)(上卷)_第5张图片

你可能感兴趣的:(Python,Python之GUI)