编程语言
首页 > 编程语言> > Python tkinter 使用

Python tkinter 使用

作者:互联网

这几天在看之前同事写的代码,他将源代码直接给到使用者,这样还要帮使用者安装环境。因此,需要把代码用pyinstaller打包成可执行文件,但是有些参数是不固定的,不能写到配置项里,所以这个时候需要用tkinter界面化来传输用户输入的参数。

主要需求

将文本框里输入的数字进行打印,如果数字修改,打印的数据也随着修改。

点击选择文件按钮的时候,会跳出文件浏览框,选择文件并映射到原窗口

大概就是这个样子。直接上源码

import tkinter as tk # tkinter是自带的库,不用下载
from tkinter import *
from tkinter import filedialog, messagebox
# 设置全局变量
stauts = None
types = None
file_path = None
entry_end_col = None
entry_end = None
entry_start_col = None
entry_start = None


def get_file_path(frame1):
    """
    获取手动选择的文件路径
    :return:
    """
    global file_path
    file_path = filedialog.askopenfilename(title='获取文件路径')
    strVar = tk.StringVar()
    strVar.set(file_path)
    label_sex = Label(frame1, textvariable=strVar, width=30)
    label_sex.grid(row=4, column=1, padx=10, pady=5)


def close():
    '''
    关闭窗口
    :return:
    '''
    print("close is called......")
    global root
    root.quit() #关闭窗口


def printInfo():
    '''
    检验参数,如果缺少参数就返回到页面
    :return:
    '''
    print("submit is called......")

    global entry_start_col, entry_end_col, \
        types, status, file_path, entry_end, entry_start
    if type(types) != str:
        if types.get() == 1:
            types = 'UDS'
        else:
            types = 'OBD'

    if type(status) != str:
        if status.get() == 1:
            status = 'TRUE'
        else:
            status = 'FALSE'
    if type(entry_start_col) != str:
        entry_start = entry_start_col.get()
    if type(entry_end_col) != str:
        entry_end = entry_end_col.get()
    if status and entry_end and entry_start and types and file_path:
        close()
    else:
        messagebox.showwarning('警告', '参数错误!请重新填写')



def radiobutton_clicked_types():
    global types


def radiobutton_clicked_status():
    '''
    Radiobutton单选按钮控件点击触发事件
    :return:
    '''
    global status


def createUI():
    '''创建窗口和控制件'''
    global root, entry_start_col, entry_end_col, types, status, file_path, entry_start, entry_end
    root = tk.Tk()
    root.title('获取参数') # 标题
    root.geometry('600x500') # 画布大小

    frame1 = tk.Frame(root) # 创建窗口
    frame1.pack()
    types = IntVar() #设置数据类型
    status = IntVar()
    v1 = IntVar()
    p1 = IntVar()
    types.set(1)
    status.set(1)

    frame1.grid(row=0, column=0)
    label_start = Label(frame1, text='开始行数:', width=10) #标签
    label_start.grid(row=0, column=0, padx=10, pady=5)
    entry_start_col = Entry(frame1, foreground='blue', width=28, textvariable=v1) #输入框
    entry_start_col.insert(0, '')
    entry_start_col.grid(row=0, column=1, padx=10, pady=5)

    label_end = Label(frame1, text='结束行数:', width=10)
    label_end.grid(row=1, column=0, padx=10, pady=5)
    entry_end_col = Entry(frame1, foreground='red', width=28, textvariable=p1)
    entry_end_col.insert(0, '')
    entry_end_col.grid(row=1, column=1, padx=10, pady=5)

    label_sex = Label(frame1, text='类型:', width=10)
    label_sex.grid(row=2, column=0, padx=10, pady=5)
    frame_sex = Frame(frame1)
    frame_sex.grid(row=2, column=1, padx=10, pady=5)

    # 1:选中, 0:不选中
    radio_btn_m = Radiobutton(frame_sex, variable=types, text='UDS', value=1,
                              command=radiobutton_clicked_types)
    radio_btn_m.grid(row=0, column=0, padx=20, pady=5)
    radio_btn_f = Radiobutton(frame_sex, variable=types, text='OBD', value=2,
                              command=radiobutton_clicked_types)
    radio_btn_f.grid(row=0, column=1, padx=20, pady=5)

    label_status = Label(frame1, text='Snap Shot status:', width=15)
    label_status.grid(row=3, column=0, padx=10, pady=5)
    frame_status = Frame(frame1)
    frame_status.grid(row=3, column=1, padx=10, pady=5)

    # 1:选中, 0:不选中
    radio_btn_n = Radiobutton(frame_status, variable=status, text='TRUE', value=1,
                              command=radiobutton_clicked_status)
    radio_btn_n.grid(row=0, column=0, padx=20, pady=5)
    radio_btn_c = Radiobutton(frame_status, variable=status, text='FALSE', value=2,
                              command=radiobutton_clicked_status)
    radio_btn_c.grid(row=0, column=1, padx=20, pady=5)

    frame1.pack(side=BOTTOM, fill=BOTH, expand=YES)
    btn_submit = Button(frame1, text='选择文件', command=lambda: get_file_path(frame1))
    btn_submit.grid(row=4, column=0, padx=20, pady=10)

    # 运行程序
    # frame_btn,其父窗口为root
    frame1.pack(side=BOTTOM, fill=BOTH, expand=YES)
    btn_submit = Button(frame1, text='运行程序', command=printInfo)
    btn_submit.grid(row=5, column=0, padx=20, pady=10)
    btn_quit = Button(frame1, text='关闭窗口', command=close)
    btn_quit.grid(row=5, column=1, padx=20, pady=10)

    root.mainloop()

    return file_path, entry_start, entry_end, types, status

createUI()

标签:status,tkinter,Python,column,grid,使用,entry,frame1,row
来源: https://blog.csdn.net/sxn777/article/details/122638451