Python Tkinter通过单击另一个按钮替换按钮的标签和命令

时间:2016-04-09 22:59:10

标签: python python-2.7 python-3.x tkinter tkinter-canvas

我正在用第二个按钮写一个科学计算器。第二个按钮的功能是什么,例如它将sin改为sin ^ -1,再加上sin按钮命令;如果再次单击第二个按钮,它会将sin ^ -1更改回sin

1 个答案:

答案 0 :(得分:0)

我会使用不同的框架将计算器分成一部分(一个用于显示计算,一个用于按钮,不具有2个功能,最后是具有2个功能的按钮)。 我将它拆分的原因是因为我会使用销毁对象并使新的对象拆分意味着你可以销毁所需的帧而不是特定的按钮(需要更少的代码)。此外,我将有3个创建GUI defs。一个是具有一个功能的按钮,另一个是显示计算的位。一个是具有2个功能的按钮(它们的第一个功能),最后是具有2个功能的按钮(它们的第二个功能)。决定使用哪个GUI gen def有一个带有全局变量的if语句,每次调用第二个函数按钮并决定使用哪个def时,它都会被更改。

如果只是命令你想要改变而不是标签和命令我将有一个变量,即etheir 1或2(当点击第二个按钮时更改)然后在你的定义中(你的按钮调用的那个)有一个if语句决定在做正常动作(例如cos)或第二动作(例如cos-1)之间。

以下是使用我在第一段中描述的代码:

from tkinter import *

class Calc(Frame):
    def __init__(self, master):
        self.modefunction = 1
        """ Initialize the frame. """
        super(Calc,self).__init__(master)
        self.grid()

        self.calculations_frm = Frame(self, width=100, height=30)#bg = "red"
        self.calculations_frm.grid(row = 0, column = 0, columnspan=2)

        self.buttons_frm = Frame(self, width= 50, height=30,)#bg = "green")
        self.buttons_frm.grid(row = 1, column = 1)

        self.buttons_2_functions_1_frm = Frame(self, width=50, height=30)#bg = "blue")
        self.buttons_2_functions_1_frm.grid(row = 1, column = 0)
        self.create_GUI()

    def create_show_calculations(self):
        self.calculation_lbl = Label(self.calculations_frm, text = "will show caluclations here").pack()

    def create_buttons(self):
        #mc stands for mode change
        self.mode_change_btn = Button(self.buttons_frm, text = "mc", command = self.mode_change, height = 1, width = 5)
        self.mode_change_btn.grid(row = 0,column = 0)

        self.plus_btn = Button(self.buttons_frm, text = "plus", height = 1, width = 5)
        self.plus_btn.grid(row = 1,column = 0)

    def create_GUI(self):
        self.create_show_calculations()
        self.create_buttons()
        self.create_1_function_gui()


    def create_1_function_gui(self):
        self.tan_btn = Button(self.buttons_2_functions_1_frm, text = "tan", height = 1, width = 5)
        self.tan_btn.grid(row = 0,column = 0)

        self.san_btn = Button(self.buttons_2_functions_1_frm, text = "san", height = 1, width = 5)
        self.san_btn.grid(row = 0,column = 1)

        self.coz_btn = Button(self.buttons_2_functions_1_frm, text = "coz", height = 1, width = 5)
        self.coz_btn.grid(row = 1,column = 0)

    def create_2_function_gui(self):
        self.buttons_2_functions_2_frm = Frame(self, width=50, height=30)#bg = "blue")
        self.buttons_2_functions_2_frm.grid(row = 1, column = 0)

        self.inverse_tan_btn = Button(self.buttons_2_functions_2_frm, text = "tan-1", height = 1, width = 5)
        self.inverse_tan_btn.grid(row = 0,column = 0)

        self.inverse_san_btn = Button(self.buttons_2_functions_2_frm, text = "san-1", height = 1, width = 5)
        self.inverse_san_btn.grid(row = 0,column = 1)

        self.inverse_coz_btn = Button(self.buttons_2_functions_2_frm, text = "coz-1", height = 1, width = 5)
        self.inverse_coz_btn.grid(row = 1,column = 0)

    def mode_change(self):
        if self.modefunction == 1:
            self.buttons_2_functions_1_frm.destroy()
            self.modefunction = 2
            self.buttons_2_functions_2_frm = Frame(self, width=50, height=30)#bg = "blue")
            self.buttons_2_functions_2_frm.grid(row = 1, column = 0)
            self.create_2_function_gui()
        else:
            self.buttons_2_functions_2_frm.destroy()
            self.modefunction =  1
            self.buttons_2_functions_1_frm = Frame(self, width=50, height=30)#bg = "blue")
            self.buttons_2_functions_1_frm.grid(row = 1, column = 0)
            self.create_1_function_gui()

root = Tk()
root.title("booking system")
root.geometry("500x500")
root.configure(bg="white")
app = Calc(root)

root.mainloop()