我正在嘗試創(chuàng)建一個(gè)執(zhí)行特定計(jì)算的 tkinter gui。我創(chuàng)建了一個(gè)窗口來(lái)要求輸入以進(jìn)行計(jì)算。但是,每次我運(yùn)行我的代碼時(shí),都會(huì)彈出 2 個(gè)窗口而不是一個(gè)。有沒(méi)有辦法在我運(yùn)行我的代碼時(shí)自動(dòng)關(guān)閉空白窗口,這樣用戶(hù)只會(huì)看到要求輸入的窗口。為簡(jiǎn)單起見(jiàn),我更改了所有按鈕以關(guān)閉應(yīng)用程序。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貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
以下兩行將創(chuàng)建一個(gè)實(shí)例,因?yàn)樗鼈儓?zhí)行時(shí)Tk()沒(méi)有實(shí)例:Tk()
s = ttk.Style() # create an instance of Tk() if there is none
s.theme_use('xpnative')
將這兩行移動(dòng)到之后,root = tk.Tk()以便它使用已經(jīng)創(chuàng)建的實(shí)例Tk():
root = tk.Tk()
s = ttk.Style() # use existing instance of Tk(), root
s.theme_use('xpnative')
添加回答
舉報(bào)
0/150
提交
取消