使用tkinter生成如下窗口:
图片.png
图片.png
图片.png
图片.png
参考资料本文最新版本地址
本文涉及的python测试开发库 谢谢点赞!
本文相关海量书籍下载
python工具书籍下载-持续更新
python GUI工具书籍下载-持续更新
#!/usr/bin/python3# -*- coding: utf-8 -*-# 技术支持:https://www.jianshu.com/u/69f40328d4f0 # 技术支持 https://china-testing.github.io/# https://github.com/china-testing/python-api-tesing/blob/master/practices/tk/tk2.py# 项目实战讨论QQ群630011153 144081101# CreateDate: 2018-11-27import tkinter as tkfrom tkinter import ttkfrom tkinter import scrolledtextfrom tkinter import Menu# Create instancewin = tk.Tk()
# Add a title win.title("Python GUI")
tabControl = ttk.Notebook(win) # Create Tab Controltab1 = ttk.frame(tabControl) # Create a tab tabControl.add(tab1, text='Tab 1') # Add the tabtab2 = ttk.frame(tabControl) # Add a second tabtabControl.add(tab2, text='Tab 2') # Make second tab visibletabControl.pack(expand=1, fill="both") # Pack to make visible# Labelframe using tab1 as the parentmighty = ttk.Labelframe(tab1, text=' Mighty Python ')
mighty.grid(column=0, row=0, padx=8, pady=4)# Modify adding a Label using mighty as the parent instead of wina_label = ttk.Label(mighty, text="Enter a name:")
a_label.grid(column=0, row=0, sticky='W')# Modified Button Click Functiondef click_me():
action.configure(text='Hello ' + name.get() + ' ' +
number_chosen.get())# Adding a Textbox Entry widgetname = tk.StringVar()
name_entered = ttk.Entry(mighty, width=12, textvariable=name)
name_entered.grid(column=0, row=1, sticky='W') # align left/West# Adding a Buttonaction = ttk.Button(mighty, text="Click Me!", command=click_me)
action.grid(column=2, row=1)
# Creating three checkbuttonsttk.Label(mighty, text="Choose a number:").grid(column=1, row=0)
number = tk.StringVar()
number_chosen = ttk.Combobox(mighty, width=12, textvariable=number, state='readonly')
number_chosen['values'] = (1, 2, 4, 42, 100)
number_chosen.grid(column=1, row=1)
number_chosen.current(0)
chVarDis = tk.IntVar()
check1 = tk.Checkbutton(mighty, text="Disabled", variable=chVarDis, state='disabled')
check1.select()
check1.grid(column=0, row=4, sticky=tk.W)
chVarUn = tk.IntVar()
check2 = tk.Checkbutton(mighty, text="UnChecked", variable=chVarUn)
check2.deselect()
check2.grid(column=1, row=4, sticky=tk.W)
chVarEn = tk.IntVar()
check3 = tk.Checkbutton(mighty, text="Enabled", variable=chVarEn)
check3.deselect()
check3.grid(column=2, row=4, sticky=tk.W)
# GUI Callback function def checkCallback(*ignoredArgs):
# only enable one checkbutton
if chVarUn.get(): check3.configure(state='disabled') else: check3.configure(state='normal') if chVarEn.get(): check2.configure(state='disabled') else: check2.configure(state='normal')
# trace the state of the two checkbuttonschVarUn.trace('w', lambda unused0, unused1, unused2 : checkCallback())
chVarEn.trace('w', lambda unused0, unused1, unused2 : checkCallback())
# Using a scrolled Text control scrol_w = 30scrol_h = 3scr = scrolledtext.ScrolledText(mighty, width=scrol_w, height=scrol_h, wrap=tk.WORD)
scr.grid(column=0, row=5, sticky='WE', columnspan=3)
# First, we change our Radiobutton global variables into a listcolors = ["Blue", "Gold", "Red"]
# We have also changed the callback function to be zero-based, using the list # instead of module-level global variables # Radiobutton Callbackdef radCall():
radSel=radVar.get()
win.configure(background=colors[radSel])# create three Radiobuttons using one variableradVar = tk.IntVar()# Next we are selecting a non-existing index value for radVarradVar.set(99)
# Now we are creating all three Radiobutton widgets within one loopfor col in range(3):
curRad = tk.Radiobutton(mighty, text=colors[col], variable=radVar,
value=col, command=radCall)
curRad.grid(column=col, row=5, sticky=tk.W) # row=5 ... SURPRISE!# Create a container to hold labelsbuttons_frame = ttk.Labelframe(mighty, text=' Labels in a frame ')
buttons_frame.grid(column=0, row=7)
# Place labels into the container elementttk.Label(buttons_frame, text="Label1").grid(column=0, row=0, sticky=tk.W)
ttk.Label(buttons_frame, text="Label2").grid(column=1, row=0, sticky=tk.W)
ttk.Label(buttons_frame, text="Label3").grid(column=2, row=0, sticky=tk.W)# Exit GUI cleanlydef _quit():
win.quit()
win.destroy()
exit()
# Creating a Menu Barmenu_bar = Menu(win)
win.config(menu=menu_bar)# Add menu itemsfile_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(label="New")
file_menu.add_separator()
file_menu.add_command(label="Exit", command=_quit)
menu_bar.add_cascade(label="File", menu=file_menu)# Add another Menu to the Menu Bar and an itemhelp_menu = Menu(menu_bar, tearoff=0)
help_menu.add_command(label="about")
menu_bar.add_cascade(label="Help", menu=help_menu)
name_entered.focus() # Place cursor into name Entry#======================# Start GUI#======================win.mainloop()
作者:python作业AI毕业设计
链接:https://www.jianshu.com/p/1e67bdab7476



