我正在嘗試創建一個執行特定計算的 tkinter gui。我創建了一個窗口來要求輸入以進行計算。但是,每次我運行我的代碼時,都會彈出 2 個窗口而不是一個。有沒有辦法在我運行我的代碼時自動關閉空白窗口,這樣用戶只會看到要求輸入的窗口。為簡單起見,我更改了所有按鈕以關閉應用程序。import numpy as npimport pandas as pdfrom datetime import datetimeimport tkinter as tkimport tkinter.ttk as ttkimport tkinter.messageboximport blpapiimport pdblpcon = pdblp.BCon(timeout=5000)con.start()s= ttk.Style()s.theme_use('xpnative')root =tk.Tk()root.title("Vega Requirement Application")root.geometry("600x400")ticker_var= tk.StringVar()volume_var= tk.StringVar()def close(): root.destroy()def clear_all(): root.destroy()def vega_calculation(): root.destroy()ticker_label = ttk.Label(root, text='Bloomberg Ticker:',font=('calibre',10,'normal'))ticker_entry = ttk.Entry(root, textvariable = ticker_var,font=('calibre',10,'normal'))volume_label = ttk.Label(root, text='Volume:',font=('calibre',10,'normal'))volume_entry = ttk.Entry(root, textvariable = volume_var,font=('calibre',10,'normal'))run_btn = ttk.Button(root, text = 'Calculate', command = vega_calculation, width = 13)close_btn = ttk.Button(root, text= 'Close App', command = close, width =13)clear_btn = ttk.Button(root, text= 'Clear table', command = clear_all, width=13)ticker_label.grid(row=0,column=0)ticker_entry.grid(row=0,column=1)volume_label.grid(row=1,column=0)volume_entry.grid(row=1,column=1)run_btn.grid(row=0,column=2)close_btn.grid(row=1, column=2)clear_btn.grid(row=0, column =4)root.mainloop()
1 回答

守候你守候我
TA貢獻1802條經驗 獲得超10個贊
以下兩行將創建一個實例,因為它們執行時Tk()沒有實例:Tk()
s = ttk.Style() # create an instance of Tk() if there is none
s.theme_use('xpnative')
將這兩行移動到之后,root = tk.Tk()以便它使用已經創建的實例Tk():
root = tk.Tk()
s = ttk.Style() # use existing instance of Tk(), root
s.theme_use('xpnative')
添加回答
舉報
0/150
提交
取消