python 制作GUI頁面以及多選框、單選框


import osimport tkinter as tk
from tkinter import filedialog
from tkinter.scrolledtext import ScrolledText

window = tk.Tk()
window.title('華潤萬家門店導出')  # 標題
window.geometry('600x500')  # 窗口尺寸

file_path = ''

contents = ScrolledText()
contents.pack(side=tk.BOTTOM, expand=True, fill=tk.BOTH)
# 輸入文本框
filename = tk.Entry()
filename.pack(side=tk.LEFT, expand=True, fill=tk.X)


# 打開文件
def open_file():
    global file_path
    global on_hit
    file_path = filedialog.askopenfilename(title=u'選擇文件', initialdir=(os.path.expanduser('H:/')))  # 這里打開文件 askdirectory(文件夾) 
    print('打開文件:', file_path)
    if file_path is not None:
        filename.insert(tk.INSERT, file_path)
        on_hit = True


on_hit = False


# 工作
def work():
    global on_hit
    if on_hit:
        print('ok')
            elif res == '登陸密碼不正確' or res == "用戶名不正常":
                print(res)
                var1.set(res)
                return
            else:
                var1.set("下載完畢!")
                return


tk.Button(text='打開文件', command=open_file).pack(side=tk.LEFT)

bt1 = tk.Button(window, text='開始運行', width=15, height=2, command=work)
bt1.place(x=250, y=150)

var1 = tk.StringVar()
label = tk.Label(window, textvariable=var1, bg="blue", font=("Arial", 12), width=25, height=2)
label.place(x=200, y=350)

window.mainloop()  # 顯示

制作多選框

import tkinter as tk

# checkButton的內容,多選


window = tk.Tk()
window.title('華潤萬家門店導出')  # 標題
window.geometry('600x500')  # 窗口尺寸

# tx = tk.Text(window,variable="")
one = tk.IntVar()
two = tk.IntVar()
three = tk.IntVar()

ck_one = tk.Checkbutton(window, text="業務單據", variable=one, offvalue=0, onvalue=1)
ck_two = tk.Checkbutton(window, text="銷售日報", variable=two, offvalue=0, onvalue=1)
ck_three = tk.Checkbutton(window, text="每日庫存", variable=three, offvalue=0, onvalue=1)

ck_one.pack()
ck_two.pack()
ck_three.pack()

on_hit = False


def work():
    print(one.get(), two.get(), three.get())  # 分別取值
    # global on_hit
    # if on_hit:
    print('ok')


bt1 = tk.Button(window, text='開始運行', width=15, height=2, command=work)
bt1.place(x=250, y=150)

tk.mainloop()

 單選框

def work():
    if on_hit:
        print(one.get())  # 取值 ,默認為0
        print(two.get())
        print(three.get())
        pass


tk.Button(text='打開文件', command=open_file).pack(side=tk.LEFT)

bt1 = tk.Button(window, text='開始運行', width=15, height=2, command=work)
bt1.place(x=250, y=200)

var1 = tk.StringVar()
label = tk.Label(window, textvariable=var1, bg="blue", font=("Arial", 12), width=25, height=2)
label.place(x=200, y=350)

one = tk.IntVar()
two = tk.IntVar()
three = tk.IntVar()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM