【基础】学习笔记25-python3 tkinter GUI编程-实操7

综合应用

界面:


代码如下:

# 综合应用

import tkinter as tk

import tkinter.ttk  # 下拉框

import tkinter.messagebox  # 弹出提示框

# =============窗口设置=============

win = tk.Tk()

win.title('考试系统注册')

win.geometry('500x300+200+200')

# ============首行:姓名=============

label1 = tk.Label(win, text='学生姓名:', justify=tk.RIGHT, width=10)

label1.grid(row=1, column=1)

name = tk.StringVar()

entry1 = tk.Entry(win, textvariable=name, width=13)

entry1.grid(row=1, column=2, pady=5)

# ===========第二行:省和市=============

data = {

    '辽宁省': ['沈阳市', '大连市', '鞍山市', '抚顺市'],

    '吉林省': ['长春市', '吉林市', '白山市'],

    '黑龙江省': ['哈尔滨市', '大庆市', '牡丹江市']

}

label21 = tk.Label(win, text='省份:', justify=tk.RIGHT, width=10)

label21.grid(row=3, column=1)

comboprince = tk.ttk.Combobox(win, values=tuple(data.keys()), width=10)  # 下拉

comboprince.grid(row=3, column=2)

label22 = tk.Label(win, text='地区:', justify=tk.RIGHT, width=10)

label22.grid(row=3, column=3)

combozone = tk.ttk.Combobox(win, width=10)

combozone.grid(row=3, column=4)

def combochange(event):

    temp = comboprince.get()

    if temp:

        combozone['values'] = data.get(temp)

    else:

        combozone.set([])

comboprince.bind('<>', combochange)  # 绑定事件

# ===========第三行:类别选择=============

# 1为本科生,0为专科生,默认1

label31 = tk.Label(win, text='请选择类别:', justify=tk.RIGHT, width=10)

label31.grid(row=5, column=1)

stutype = tk.IntVar()

stutype.set(1)  # 默认1

radio1 = tk.Radiobutton(win, variable=stutype, value=1, text='本科学生')

radio2 = tk.Radiobutton(win, variable=stutype, value=0, text='专科学生')

radio1.grid(row=5, column=2, pady=5)

radio2.grid(row=5, column=3, pady=5)

# ===========第四行:判断+按钮=============

# 与英语专业是否相关;增加和删除按钮

major = tk.IntVar()

major.set(0)  # 默认不选

majorcheck = tk.Checkbutton(win,

                            variable=major,

                            text='是否英语专业?',

                            onvalue=1,

                            offvalue=0,

                            justify=tk.RIGHT,

                            width=20)

majorcheck.grid(row=7, column=1, pady=5)

# 按钮

def addInfotmation():

    result = '学生名字:' + entry1.get()

    result += ';省份:' + comboprince.get()

    result += ';地区:' + combozone.get()

    result += ';类别:' + ('本科学生' if stutype.get() else '专科学生')

    result += ';英语专业' + ('YES' if major.get() else 'NO')

    listboxstudent.insert(0, result)

def deleteSelection():

    selection = listboxstudent.curselection()

    if not selection:  # 没有选中,弹出没选中提示框

        tk.messagebox.showinfo(title='Information', message='No Selection')

    else:

        listboxstudent.delete(selection)

btadd = tk.Button(win,

                  text='增加',

                  width=10,

                  command=addInfotmation)

btdelete = tk.Button(win,

                    text='删除',

                    width=10,

                    command=deleteSelection)

btadd.grid(row=7, column=2)

btdelete.grid(row=7, column=3)

# ===========第五行:列表框组件=============

listboxstudent = tk.Listbox(win, width=60)

listboxstudent.grid(row=8, column=1, columnspan=4, pady=5)

# 创建列表框与两按钮的关联

win.mainloop()

你可能感兴趣的:(【基础】学习笔记25-python3 tkinter GUI编程-实操7)