苏宇117
2021-02-21 19:19
采纳率: 100%
浏览 206

Python tkinter Button触发问题

我准备做一个小软件:

是这样的我做了一个主窗口:主窗口设置一个按键,点击后再创建一个窗口,填一些信息,然后设置了一个按钮,去提交这些信息

相当与做了一个简易对话框。

现在的问题是:我点击主窗口按钮是,对话框的按键函数都实现了,按对话框的按键却不执行,代码在下,希望大佬们解惑。

这个局怎么破?

import tkinter
import tkinter.messagebox
import os

my_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()
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • Miku丨无形 2021-02-22 16:34
    已采纳
    import tkinter
    
    import tkinter.messagebox
    
    import os
    
    my_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()
    点赞 打赏 评论
  • Miku丨无形 2021-02-22 16:35

    提交1和提交2 的点击事件  要用匿名函数  才能传参

    点赞 打赏 评论
  • Miku丨无形 2021-02-22 16:36

    还有就是  新窗口弹出  后面的会被覆盖过去  挪出来就好了

    点赞 打赏 评论

相关推荐 更多相似问题