提交按钮在python3中不能正常工作。如何解决?

2024-04-26 04:12:00 发布

您现在位置:Python中文网/ 问答频道 /正文

import tkinter as tk
from tkinter import *
import  tkinter.ttk

class window1( Frame ):
    def __init__( self ):
        tk.Frame.__init__(self)
        self.pack()
        self.master.title("MANAGEMENT")

        self.button1 = Button( self, text = "ENTRY", width = 25,command = self.GUI)                        
        self.button1.grid( row = 0, column = 1, columnspan = 2, sticky = W+E+N+S )

        self.button2 = Button( self, text = "SHOW BILL", width = 25 )
        self.button2.grid( row = 1, column = 1, columnspan = 2, sticky = W+E+N+S )

        self.button3 = Button( self, text = "MEMBERS", width = 25)                      
        self.button3.grid( row = 2, column =1 , columnspan = 2, sticky = W+E+N+S )

    def GUI(self):
        self.GUI= GUI()


class GUI(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.buttonDic = {
        '1':0,
        '2':0,
        '3':0,
        '4':0
        }

        for key in self.buttonDic:
            self.buttonDic[key] = tk.IntVar()
            aCheckButton = tk.Checkbutton(self, text=key,
                                            variable=self.buttonDic[key])
            aCheckButton.grid(sticky='w')

        submitButton = tk.Button(self, text="Submit",
                                        command=self.query_checkbuttons)
        submitButton.grid()

    def query_checkbuttons(self):
        for key, value in self.buttonDic.items():
            state = value.get()
            if state != 0:
                print(key)
                self.buttonDic[key].set(0)



def main(): 
    window1().mainloop()

if __name__ == '__main__':
    main()

Tags: keytextimportselfinittkinterdefgui
1条回答
网友
1楼 · 发布于 2024-04-26 04:12:00

您的问题是在代码中运行了两个Tk()实例。当您初始化没有父级的框架时,第一个隐藏在window1调用中,第二个隐藏在GUI类中。由于没有指定IntVar的父级,它们与第一个创建的窗口相关联,因此不是正确的窗口。你知道吗

我通过以下方式修改了您的代码:

  • 我使Window1类从Tk继承,因为您将它用作主窗口
  • 我让您的GUI类从Toplevel继承,以避免程序中有两个正在运行的Tk实例
  • 我指定了IntVar父级,因此没有歧义

代码如下:

import tkinter as tk

class Window1(tk.Tk):  # inherit from Tk instead of Frame
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("MANAGEMENT")

        self.button1 = tk.Button(self, text="ENTRY", width=25,command = self.GUI)                        
        self.button1.grid(row=0, column=1, columnspan=2, sticky="wesn" )

        self.button2 = tk.Button(self, text="SHOW BILL", width=25 )
        self.button2.grid(row=1, column=1, columnspan=2, sticky="wesn")

        self.button3 = tk.Button(self, text="MEMBERS", width=25)                      
        self.button3.grid(row=2, column=1 , columnspan=2, sticky="wesn")

    def GUI(self):
        self.GUI= GUI(self)


class GUI(tk.Toplevel):  # inherit from Toplevel instead of Tk
    def __init__(self, parent):
        tk.Toplevel.__init__(self, parent)

        self.buttonDic = {
        '1':0,
        '2':0,
        '3':0,
        '4':0
        }

        for key in self.buttonDic:
            self.buttonDic[key] = tk.IntVar(self)  # specified the parent of IntVar explicitly
            aCheckButton = tk.Checkbutton(self, text=key,
                                          variable=self.buttonDic[key])
            aCheckButton.grid(sticky='w')

        submitButton = tk.Button(self, text="Submit",
                                 command=self.query_checkbuttons)
        submitButton.grid()

    def query_checkbuttons(self):
        for key, value in self.buttonDic.items():
            state = value.get()
            if state != 0:
                print(key)
                self.buttonDic[key].set(0)


def main(): 
    Window1().mainloop()

if __name__ == '__main__':
    main()

相关问题 更多 >