Python tkinter Button觸發問題

CSDN問答 2022-01-07 17:40:39 阅读数:346

python tkinter button

我准備做一個小軟件:

是這樣的我做了一個主窗口:主窗口設置一個按鍵,點擊後再創建一個窗口,填一些信息,然後設置了一個按鈕,去提交這些信息

相當與做了一個簡易對話框。

現在的問題是:我點擊主窗口按鈕是,對話框的按鍵函數都實現了,按對話框的按鍵卻不執行,代碼在下,希望大佬們解惑。

這個局怎麼破?

import tkinterimport tkinter.messageboximport osmy_filename = "myfile.tsl"class TSL_GUI(): def __init__(self,init_windows_name): self.windows_name = init_windows_name # 看看是否需要創建一個新的物料系統 self.isChuan = os.path.exists(my_filename) # try: # self.filename = open(my_filename,"w+") # except: # print("文件打開錯誤") self.filename = None self.i = 0; def set_window(self): self.windows_name.title("物料統計工具") self.windows_name.geometry('720x480+200+200') if self.isChuan == False: self.buttonChuan = tkinter.Button(self.windows_name,width = 10,bd = 0,text="創建",command = self.chuanJian) self.buttonChuan.pack() else: self.buttonJia = tkinter.Button(self.windows_name,width = 10,bd = 0,text="+") self.buttonJian = tkinter.Button(self.windows_name,width = 10,bd = 0,text="-") self.buttonJia.pack() self.buttonJian.pack() def closefile(self): if(self.filename != None): self.filename.close() def chuanJian(self): chuan = tkinter.Tk() chuan.title("創建物料系統") chuan.geometry('360x120+240+240') label_layer = tkinter.Label(chuan,width = 10,text="層:") label_layer.grid(row=0,column = 0) entry_layer = tkinter.Entry(chuan,width = 30) entry_layer.grid(row=0,column = 1,columnspan='2') label_height = tkinter.Label(chuan,width = 10,text="長:") label_height.grid(row=1,column = 0) entry_height = tkinter.Entry(chuan,width = 30,text="sha") entry_height.grid(row=1,column = 1,columnspan='2') label_width = tkinter.Label(chuan,width = 10,text="寬:") label_width.grid(row=2,column = 0) entry_width = tkinter.Entry(chuan,width = 30) entry_width.grid(row=2,column = 1,columnspan='2') button_submit = tkinter.Button(chuan,bd = 0,text="提交",state = "normal",command = self.chuanJian_sub(entry_layer,entry_height,entry_width)) button_submit.grid(row=3,column=1) button_submit = tkinter.Button(chuan,bd = 0,text="提交2",state = "active",command = self.chuanJian_sub2(entry_layer,entry_height,entry_width)) button_submit.grid(row=3,column=2) chuan.mainloop() def chuanJian_sub(self,entry_l,entry_h,entry_w): tkinter.messagebox.showinfo("內容",entry_l.get()) self.i+=1 entry_l.insert(0,str(self.i)) def chuanJian_sub2(self,entry_l,entry_h,entry_w): tkinter.messagebox.showinfo("內容2",entry_h.get()) self.i+=1 entry_h.insert(0,str(self.i))def gui_start(): init_window = tkinter.Tk() mgui=TSL_GUI(init_window) mgui.set_window() init_window.mainloop() mgui.closefile()gui_start()



采納答案:
import tkinterimport tkinter.messageboximport osmy_filename = "myfile.tsl"class TSL_GUI(): def __init__(self, init_windows_name): self.windows_name = init_windows_name # 看看是否需要創建一個新的物料系統 self.isChuan = os.path.exists(my_filename) # try: # self.filename = open(my_filename,"w+") # except: # print("文件打開錯誤") self.filename = None self.i = 0; def set_window(self): self.windows_name.title("物料統計工具") self.windows_name.geometry('720x480+200+200') if self.isChuan == False: self.buttonChuan = tkinter.Button(self.windows_name, width=10, bd=0, text="創建", command=self.chuanJian) self.buttonChuan.pack() else: self.buttonJia = tkinter.Button(self.windows_name, width=10, bd=0, text="+") self.buttonJian = tkinter.Button(self.windows_name, width=10, bd=0, text="-") self.buttonJia.pack() self.buttonJian.pack() def closefile(self): if (self.filename != None): self.filename.close() def chuanJian(self): chuan = tkinter.Tk() chuan.title("創建物料系統") chuan.geometry('360x120+240+240') label_layer = tkinter.Label(chuan, width=10, text="層:") label_layer.grid(row=0, column=0) entry_layer = tkinter.Entry(chuan, width=30) entry_layer.grid(row=0, column=1, columnspan='2') label_height = tkinter.Label(chuan, width=10, text="長:") label_height.grid(row=1, column=0) entry_height = tkinter.Entry(chuan, width=30, text="sha") entry_height.grid(row=1, column=1, columnspan='2') label_width = tkinter.Label(chuan, width=10, text="寬:") label_width.grid(row=2, column=0) entry_width = tkinter.Entry(chuan, width=30) entry_width.grid(row=2, column=1, columnspan='2') button_submit = tkinter.Button(chuan, bd=0, text="提交", state="normal", command=lambda: self.chuanJian_sub(entry_layer, entry_height, entry_width)) button_submit.grid(row=3, column=1) button_submit = tkinter.Button(chuan, bd=0, text="提交2", state="active", command=lambda: self.chuanJian_sub2(entry_layer, entry_height, entry_width)) button_submit.grid(row=3, column=2) chuan.mainloop() def chuanJian_sub(self, entry_l, entry_h, entry_w): tkinter.messagebox.showinfo("內容", entry_l.get()) self.i += 1 entry_l.insert(0, str(self.i)) def chuanJian_sub2(self, entry_l, entry_h, entry_w): tkinter.messagebox.showinfo("內容2", entry_h.get()) self.i += 1 entry_h.insert(0, str(self.i))def gui_start(): init_window = tkinter.Tk() mgui = TSL_GUI(init_window) mgui.set_window() init_window.mainloop() mgui.closefile()gui_start()


其他答案2:

提交1和提交2 的點擊事件  要用匿名函數  才能傳參


其他答案3:

還有就是  新窗口彈出  後面的會被覆蓋過去  挪出來就好了

版权声明:本文为[CSDN問答]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201071740387539.html