直接上代碼
import xlwings as xw
from mytkinter import *
window = tk.Tk()
window.title('測試視窗')
window.geometry('500x300')
class Oc():
def __init__(self):
self.app=xw.App(visible=True,add_book=False)
def open_excel(self):
filepath=get_path()
wb=self.app.books.open(filepath)
def close_excel(self):
wb=xw.books.active
wb.close()
oc=Oc()
b=tk.Button(window,text='打開工作簿',width=10,
height=2,command=oc.open_excel)
b.pack(side=tk.TOP)
b1=tk.Button(window,text='關閉工作簿',width=10,height=2,command=oc.close_excel)
b1.pack(side=tk.TOP)
window.mainloop()
原來button的command後面不僅可以放函數,還可以放執行個體的方法,打通了這個環節,就為GUI程式設計打通的道路。