在 ttkbootstrap 的一个类中检测 witgets

问题描述 投票:0回答:1

我正在用 python 开发一个科学计算器,有一个按钮我想用来更改屏幕上的其他按钮(我想要 shift 按钮将 sin、cos 和 tan 更改为 arcsin、arccos、arctan)。


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


class Master:
    def __init__(self) -> None:
        self.root = ttk.Window(title= "Calculator", themename= "darkly", size=(400,500))
        self.state = True
    
    def print_numbers(self):
        for i, rows in enumerate(self.numbers):
            self.frame_for_the_buttons = ttk.Frame(self.root)
            self.frame_for_the_buttons.pack(fill= BOTH,expand=True)
            for j, buttons in enumerate(rows):
                if buttons in ("shift","**","sqrt","e","π","±","M"):
                    butt = ttk.Button(master= self.frame_for_the_buttons, text=buttons, command= lambda vl=self.numbers[i][j]: self.claculate(vl), style= (WARNING,LINK))
                    butt.pack(side=LEFT, expand=True, fill=BOTH)
                elif buttons in ("deg","sin","cos","tan","log","ln","(",")"):
                    butt = ttk.Button(master= self.frame_for_the_buttons, text=buttons, command= lambda vl=self.numbers[i][j]: self.claculate(vl), style= (SUCCESS,LINK))
                    butt.pack(side=LEFT, expand=True, fill=BOTH)
                elif buttons in ("ac","c","%","/","x","-","+","="):
                    butt = ttk.Button(master= self.frame_for_the_buttons, text=buttons, command= lambda vl=self.numbers[i][j]: self.claculate(vl), style= (INFO,LINK))
                    butt.pack(side=LEFT, expand=True, fill=BOTH)
                else:
                    butt = ttk.Button(master= self.frame_for_the_buttons, text=buttons, command= lambda vl=self.numbers[i][j]: self.claculate(vl), style= (LIGHT, LINK))
                    butt.pack(side=LEFT, expand=True, fill=BOTH)
    
    def claculate(self, num):
        if num == "shift":
            self.state = not(self.state)
            if self.state:
                self.frame_for_the_buttons.destroy()
                self.numbers = [("shift","deg","sin","cos","tan"),
                                ("**","log","ln","(",")"),
                                ("sqrt","ac","c","%","/"),
                                ("e","7","8","9","x"),
                                ("π","4","5","6","-"),
                                ("±","1","2","3","+"),
                                ("M","0",",",".","=")]
                self.print_numbers()
            else:
                self.frame_for_the_buttons.destroy()
                self.numbers = [("shift","deg","arcsin","arccos","arctan"),
                                ("**","log","ln","(",")"),
                                ("sqrt","ac","c","%","/"),
                                ("e","7","8","9","x"),
                                ("π","4","5","6","-"),
                                ("±","1","2","3","+"),
                                ("M","0",",",".","=")]
                self.print_numbers()
    
    def mainloop(self):
        #Entry for numbers and operations
        self.entry_operation = ttk.Entry(master= self.root, style="dark", font= "Arial 40 bold")
        self.entry_operation.pack(side = TOP, fill= X, expand= True)
        
        #Numbers
        self.numbers = [("shift","deg","sin","cos","tan"),
                        ("**","log","ln","(",")"),
                        ("sqrt","ac","c","%","/"),
                        ("e","7","8","9","x"),
                        ("π","4","5","6","-"),
                        ("±","1","2","3","+"),
                        ("M","0",",",".","=")]
        self.print_numbers()
        self.root.mainloop()        
        
win = Master()
win.mainloop()

我尝试过使用

.destry()
.pack_forget()
但是没有一种方法有效......我也想知道是否有更有效的方法来完成任务而不是删除所有按钮并用我想要的按钮替换它们即使我只换3.

python button interface ttk
1个回答
0
投票

我找到了一个解决方案,就是为整个事物创建另一个框架并销毁那个框架,出于某种原因,我之前提出的方法没有...

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


class Master:
    def __init__(self) -> None:
        self.root = ttk.Window(title= "Calculator", themename= "darkly", size=(400,500))
        self.state = True
    
    def print_numbers(self):
        self.frame_for_ev = ttk.Frame(self.root)
        self.frame_for_ev.pack(fill= BOTH,expand=True)
        for i, rows in enumerate(self.numbers):
            self.frame_for_the_buttons = ttk.Frame(self.frame_for_ev)
            self.frame_for_the_buttons.pack(fill= BOTH,expand=True)
            for j, buttons in enumerate(rows):
                if buttons in ("shift","**","sqrt","e","π","±","M"):
                    butt = ttk.Button(master= self.frame_for_the_buttons, text=buttons, command= lambda vl=self.numbers[i][j]: self.claculate(vl), style= (WARNING,LINK))
                    butt.pack(side=LEFT, expand=True, fill=BOTH)
                elif buttons in ("deg","sin","cos","tan","log","ln","(",")"):
                    butt = ttk.Button(master= self.frame_for_the_buttons, text=buttons, command= lambda vl=self.numbers[i][j]: self.claculate(vl), style= (SUCCESS,LINK))
                    butt.pack(side=LEFT, expand=True, fill=BOTH)
                elif buttons in ("ac","c","%","/","x","-","+","="):
                    butt = ttk.Button(master= self.frame_for_the_buttons, text=buttons, command= lambda vl=self.numbers[i][j]: self.claculate(vl), style= (INFO,LINK))
                    butt.pack(side=LEFT, expand=True, fill=BOTH)
                else:
                    butt = ttk.Button(master= self.frame_for_the_buttons, text=buttons, command= lambda vl=self.numbers[i][j]: self.claculate(vl), style= (LIGHT, LINK))
                    butt.pack(side=LEFT, expand=True, fill=BOTH)
    
    def claculate(self, num):
        if num == "shift":
            self.state = not(self.state)
            if self.state:
                self.frame_for_ev.destroy()
                self.numbers = [("shift","deg","sin","cos","tan"),
                                ("**","log","ln","(",")"),
                                ("sqrt","ac","c","%","/"),
                                ("e","7","8","9","x"),
                                ("π","4","5","6","-"),
                                ("±","1","2","3","+"),
                                ("M","0",",",".","=")]
                self.print_numbers()
            else:
                self.frame_for_ev.destroy()
                self.numbers = [("shift","deg","arcsin","arccos","arctan"),
                                ("**","log","ln","(",")"),
                                ("sqrt","ac","c","%","/"),
                                ("e","7","8","9","x"),
                                ("π","4","5","6","-"),
                                ("±","1","2","3","+"),
                                ("M","0",",",".","=")]
                self.print_numbers()
    
    def mainloop(self):
        #Entry for numbers and operations
        self.entry_operation = ttk.Entry(master= self.root, style="dark", font= "Arial 40 bold")
        self.entry_operation.pack(side = TOP, fill= X, expand= True)
        
        #Numbers
        self.numbers = [("shift","deg","sin","cos","tan"),
                        ("**","log","ln","(",")"),
                        ("sqrt","ac","c","%","/"),
                        ("e","7","8","9","x"),
                        ("π","4","5","6","-"),
                        ("±","1","2","3","+"),
                        ("M","0",",",".","=")]
        self.print_numbers()
        self.root.mainloop()        
        
win = Master()
win.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.