使用按钮使多选项卡tkinter应用程序正常工作

时间:2019-07-11 22:26:24

标签: python python-3.x tkinter

我有一个使用Python tkinter的小型测试应用程序,可以开始工作。问题是,当按下“退出”按钮时,它不能正确退出。这是一个两帧的选项卡式应用程序,在这里我从StackOverflow问题ttk tkinter multiple frames/windows开始。

现在这是一个完整的示例,可以工作,但是需要工作,因为它无法正常退出和退出。当我按下“退出”按钮时,它将终止该选项卡的框架,但是应用程序无法退出并正确退出。我必须点击“ X”窗口关闭图标以将其关闭。

我的主要问题是如何(在哪里?)如何在“英尺到米”计算器上的“退出”按钮或BMI计算器上的“取消/退出”按钮上测试事件。

第二个问题是应用程序的设计对我而言似乎效率低下,因为它创建了两个“框架”小部件,每个对象都有自己的一组按钮,包括2个“退出”按钮。如何将这些选项卡和框架放入父窗口,然后在该父窗口上添加退出按钮以关闭整个应用程序。

我修改了按钮以正确破坏按钮所在的框架:

将button2的“ command = self.quit”更改为“ command = self.destroy”

self.button2 = ttk.Button(self,text =“ Cancel / Quit”,command = self.quit).grid(row = 3,column = 1,sticky = E)

self.button2 = ttk.Button(self, text="Cancel/Quit", command=self.destroy).grid(row=3, column=1, sticky=E)

""" Created on Thu Jul 11 17:20:22 2019 """

from tkinter import *
from tkinter import ttk
from tkinter import messagebox

class App1(ttk.Frame):
    """ This application calculates BMI and returns a value. """ 

    def __init__(self, master=None):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):
        #text variables
        self.i_height = StringVar()
        self.i_weight = StringVar()
        self.o_bmi = StringVar()

        #labels
        self.label1 = ttk.Label(self, text="Enter your weight:").grid(row=0, column=0, sticky=W)
        self.label2 = ttk.Label(self, text="Enter your height:").grid(row=1, column=0, sticky=W)
        self.label3 = ttk.Label(self, text="Your BMI is:").grid(row=2, column=0, sticky=W)

        #text boxes
        self.textbox1 = ttk.Entry(self, textvariable=self.i_weight).grid(row=0, column=1, sticky=E)
        self.textbox2 = ttk.Entry(self, textvariable=self.i_height).grid(row=1, column=1, sticky=E)
        self.textbox3 = ttk.Entry(self, textvariable=self.o_bmi).grid(row=2, column=1, sticky=E)

        #buttons
        self.button1 = ttk.Button(self, text="Ok", command=self.calculateBmi).grid(row=3, column=2, sticky=E)
## Changed button2 "command=self.quit"  to  "command=self.destroy"
#        self.button2 = ttk.Button(self, text="Cancel/Quit", command=self.quit).grid(row=3, column=1, sticky=E)
        self.button2 = ttk.Button(self, text="Cancel/Quit", command=self.destroy).grid(row=3, column=1, sticky=E)

#exitApplication = tk.Button(root, text='Exit Application', command=root.destroy)
#canvas1.create_window(85, 300, window=exitApplication) 


    def calculateBmi(self):
        try:
            self.weight = float(self.i_weight.get())
            self.height = float(self.i_height.get())
            self.bmi = self.weight / self.height ** 2.0
            self.o_bmi.set(self.bmi)
        except ValueError:
            messagebox.showinfo("Error", "You can only use numbers.")
        finally:
            self.i_weight.set("")
            self.i_height.set("")

class App2(ttk.Frame):
    """ Application to convert feet to meters or vice versa. """
    def __init__(self, master=None):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """Create the widgets for the GUI"""
        # 1 textbox (stringvar)
        self.entry= StringVar()
        self.textBox1= ttk.Entry(self, textvariable=self.entry).grid(row=0, column=1)

        # 5 labels (3 static, 1 stringvar)
        self.displayLabel1 = ttk.Label(self, text="feet").grid(row=0, column=2, sticky=W)
        self.displayLabel2 = ttk.Label(self, text="is equivalent to:").grid(row=1, column=0)
        self.result= StringVar()
        self.displayLabel3 = ttk.Label(self, textvariable=self.result).grid(row=1, column=1)
        self.displayLabel4 = ttk.Label(self, text="meters").grid(row=1, column=2, sticky=W)

        # 2 buttons
        self.calculateButton = ttk.Button(self, text="Calculate", command=self.convert_feet_to_meters).grid(row=2, column=2, sticky=(S,E))
        self.quitButton = ttk.Button(self, text="Quit", command=self.destroy).grid(row=2, column=1, sticky=(S,E))

#exitApplication = tk.Button(root, text='Exit Application', command=root.destroy)
#canvas1.create_window(85, 300, window=exitApplication) 


    def convert_feet_to_meters(self):
        """Converts feet to meters, uses string vars and converts them to floats"""
        self.measurement = float(self.entry.get())
        self.meters = self.measurement * 0.3048
        self.result.set(self.meters)

###  CODE BELOW COMMENTED OUT WHEN JOINING ORIGINAL POSTER CODE WITH HIS SOLUTION
### It seems no longer relevant since App1 and App2 have their own buttons.

#def button1_click():
#    """ This is for the BMI Calculator Widget """
#    root = Tk()
#    app = App1(master=root)
#    app.mainloop()
#
#def button2_click():
#    """ This is for the Feet to Meters Conversion Widget """
#    root = Tk()
#    app = App2(master=root)
#    app.mainloop()

#def main():
#    window = Tk()
#    button1 = ttk.Button(window, text="bmi calc", command=button1_click).grid(row=0, column=1)
#    button2 = ttk.Button(window, text="feet conv", command=button2_click).grid(row=1, column=1)
#    window.mainloop()


def main():
    #Setup Tk()
    window = Tk()

    #Setup the notebook (tabs)
    notebook = ttk.Notebook(window)
    frame1 = ttk.Frame(notebook)
    frame2 = ttk.Frame(notebook)
    notebook.add(frame1, text="BMI Calc")
    notebook.add(frame2, text="Feet to Meters")
    notebook.grid()

    #Create tab frames
    app1 = App1(master=frame1)
    app1.grid()
    app2 = App2(master=frame2)
    app2.grid()

    #Main loop
    window.mainloop()

if __name__ == '__main__':
    main()

按下“退出”按钮时,应用程序不会退出。仅单个帧退出。

1 个答案:

答案 0 :(得分:0)

感谢Martineau的提示,该提示可以帮助我使此示例生效。我将“窗口”声明为全局变量,因为它是在类构造函数的名称空间中定义的。否则,会出现未定义窗口的错误。此方法通过将window作为全局变量来传递,从而破坏了类的封装和模块化。如果有更好的方法可以做到这一点,我想知道。

    # -*- coding: utf-8 -*-
""" Created on Thu Jul 11 17:20:22 2019   
    Filename: tkinter-multiple-frames-windows_v3.py
    From question on StackOverflow question "ttk tkinter multiple frames/windows"
    at https://stackoverflow.com/questions/6035101/ttk-tkinter-multiple-frames-windows?rq=1
    Now a full example that works but it needed some modification to clarify how it works.
"""

from tkinter import *
from tkinter import ttk
from tkinter import messagebox

class BMICalcApp(ttk.Frame):
    """ This application calculates BMI and returns a value. """ 

    def __init__(self, master=None):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):
        #text variables
        self.i_height = StringVar()
        self.i_weight = StringVar()
        self.o_bmi = StringVar()

        #labels
        self.label1 = ttk.Label(self, text="Enter your weight:").grid(row=0, column=0, sticky=W)
        self.label2 = ttk.Label(self, text="Enter your height:").grid(row=1, column=0, sticky=W)
        self.label3 = ttk.Label(self, text="Your BMI is:").grid(row=2, column=0, sticky=W)

        #text boxes
        self.textbox1 = ttk.Entry(self, textvariable=self.i_weight).grid(row=0, column=1, sticky=E)
        self.textbox2 = ttk.Entry(self, textvariable=self.i_height).grid(row=1, column=1, sticky=E)
        self.textbox3 = ttk.Entry(self, textvariable=self.o_bmi).grid(row=2, column=1, sticky=E)

        #buttons
        self.button1 = ttk.Button(self, text="Ok", command=self.calculateBmi).grid(row=3, column=2, sticky=E)
        self.button2 = ttk.Button(self, text="Cancel/Quit", command=window.destroy).grid(row=3, column=1, sticky=E)

    def calculateBmi(self):
        try:
            self.weight = float(self.i_weight.get())
            self.height = float(self.i_height.get())
            self.bmi = self.weight / self.height ** 2.0
            self.o_bmi.set(self.bmi)
        except ValueError:
            messagebox.showinfo("Error", "You can only use numbers.")
        finally:
            self.i_weight.set("")
            self.i_height.set("")

class ConvertFeetMeters(ttk.Frame):
    """ Application to convert feet to meters or vice versa. """
    def __init__(self, master=None):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """Create the widgets for the GUI"""
        # 1 textbox (stringvar)
        self.entry= StringVar()
        self.textBox1= ttk.Entry(self, textvariable=self.entry).grid(row=0, column=1)

        # 5 labels (3 static, 1 stringvar)
        self.displayLabel1 = ttk.Label(self, text="feet").grid(row=0, column=2, sticky=W)
        self.displayLabel2 = ttk.Label(self, text="is equivalent to:").grid(row=1, column=0)
        self.result= StringVar()
        self.displayLabel3 = ttk.Label(self, textvariable=self.result).grid(row=1, column=1)
        self.displayLabel4 = ttk.Label(self, text="meters").grid(row=1, column=2, sticky=W)

        # 2 buttons
        self.calculateButton = ttk.Button(self, text="Calculate", command=self.convert_feet_to_meters).grid(row=2, column=2, sticky=(S,E))
        self.quitButton = ttk.Button(self, text="Quit", command=window.destroy).grid(row=2, column=1, sticky=(S,E))

    def convert_feet_to_meters(self):
        """Converts feet to meters, uses string vars and converts them to floats"""
        self.measurement = float(self.entry.get())
        self.meters = self.measurement * 0.3048
        self.result.set(self.meters)


def main():
    #Setup Tk()
    global window 
    window = Tk()

    #Setup the notebook (tabs)
    notebook = ttk.Notebook(window)
    frame1 = ttk.Frame(notebook)
    frame2 = ttk.Frame(notebook)
    notebook.add(frame1, text="BMI Calc")
    notebook.add(frame2, text="Feet to Meters")
    notebook.grid()

    #Create tab frames
    bmi_calc = BMICalcApp(master=frame1)
    bmi_calc.grid()
    feet_meters_calc = ConvertFeetMeters(master=frame2)
    feet_meters_calc.grid()

    #Main loop
    window.mainloop()

if __name__ == '__main__':
    main()