首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >似乎无法让.config在Tkinter中实现自更新显示

似乎无法让.config在Tkinter中实现自更新显示
EN

Stack Overflow用户
提问于 2022-10-11 13:39:27
回答 1查看 41关注 0票数 -1

我正在尝试学习python,并尝试新的项目,在这种情况下,一个迷你计算器。我一直想要创建一个“自更新”标签,用于打印输入到计算器中的输入,例如: 5+5。我试图使用与我持有的字符串相同的变量“计算”来完成这一任务,该字符串后来是用eval()函数计算的。我看到了几个使用".config“和"after”的例子,但我似乎无法让它起作用。如果有人能帮我,我会很感激的,谢谢!

代码语言:javascript
复制
import tkinter

from tkinter import Button, Tk, ttk, messagebox
root = Tk()

root.geometry("300x150+600+250")
root.configure(background="black")

Computation = ""
VarDisplay = Computation


def Display():
    global Computation
    VarDisplay.config(Computation=Computation)
    root.after(1000, Display)
def Clear():
    global Computation
    Computation = ""
def Answer ():
    global Computation
    Computation = eval(Computation)
    tkinter.messagebox.showinfo("Answer",Computation)
    Computation = ""
def Trolling ():
    tkinter.messagebox.showinfo("Greetings", "Hello :)")
def OneFunc ():
    global Computation
    Computation += "1" 
def TwoFunc ():
    global Computation
    Computation += "2"
def ThreeFunc ():
    global Computation
    Computation += "3"
def FourFunc ():
    global Computation
    Computation += "4"
def FiveFunc ():
    global Computation
    Computation += "5"
def SixFunc ():
    global Computation
    Computation += "6"
def SevenFunc ():
    global Computation
    Computation += "7"
def EightFunc ():
    global Computation
    Computation += "8"
def NineFunc ():
    global Computation
    Computation += "9"
def ZeroFunc ():
    global Computation
    Computation += "0"
def AdditionFunc ():
    global Computation
    Computation += "+"
def MultiplicationFunc ():
    global Computation
    Computation += "*"
def SubtractionFunc ():
    global Computation
    Computation += "-"
def DivisionFunc ():
    global Computation
    Computation += "/"
def PlusMinusFunc ():
    global Computation
    Computation += "(-)"
def PercentageFunc ():
    global Computation
    Computation += "/100 *"
def DecimalFunc ():
    global Computation
    Computation += "."



test = tkinter.Label(root, text="Calc", width = 8).grid(column=0, row=0)
VarDisplay = tkinter.Label(root, text=VarDisplay, width = 20,bg='black',fg='White').grid(column=1,row=0,columnspan=3)
ACButton = ttk.Button(root, text = "AC", width = 5, command = Clear).grid(column=0, row=1)
PlusMinusButton = ttk.Button(root, text = "+ -",   width = 5, command = PlusMinusFunc).grid(column=1, row=1)
PercentageButton = ttk.Button(root, text = "%",   width = 5, command = PercentageFunc).grid(column=2, row=1)
DivisionButton = ttk.Button(root, text = "/",   width = 5, command = DivisionFunc).grid(column=3, row=1)
SevenButton = ttk.Button(root, text = "7",   width = 5, command = SevenFunc).grid(column=0, row=2)
EightButton = ttk.Button(root, text = "8",   width = 5, command = EightFunc).grid(column=1, row=2)
NineButton = ttk.Button(root, text = "9",   width = 5, command = NineFunc).grid(column=2, row=2)
MultiplicationButton = ttk.Button(root, text = "*",  width = 5,  command = MultiplicationFunc).grid(column=3, row=2)
FourButton = ttk.Button(root, text = "4",   width = 5, command = FourFunc).grid(column=0, row=3)
FiveButton = ttk.Button(root, text = "5",   width = 5, command = FiveFunc).grid(column=1, row=3)
SixButton = ttk.Button(root, text = "6",   width = 5, command = SixFunc).grid(column=2, row=3)
MinusButton = ttk.Button(root, text = "-",   width = 5, command = SubtractionFunc).grid(column=3, row=3)
OneButton = ttk.Button(root, text = "1",   width = 5, command = OneFunc).grid(column=0, row=4)
TwoButton = ttk.Button(root, text = "2",   width = 5, command = TwoFunc).grid(column=1, row=4)
ThreeButton = ttk.Button(root, text = "3",   width = 5, command = ThreeFunc).grid(column=2, row=4)
AdditionButton = ttk.Button(root, text = "+",   width = 5, command = AdditionFunc).grid(column=3, row=4)
ZeroButton = ttk.Button(root, text = "0",   width = 5, command = ZeroFunc).grid(column=0, row=5)
DecimalButton = ttk.Button(root, text = ".",   width = 5, command = DecimalFunc).grid(column=1, row=5)
EqualButton = ttk.Button(root, text = "=",   width = 5, command = Answer).grid(column=2, row=5)
RandomButton = ttk.Button(root, text = ":)",    width = 5, command = Trolling).grid(column=3, row=5)

Display()
root.mainloop()
EN

回答 1

Stack Overflow用户

发布于 2022-10-12 15:44:24

这个工作得更深入一点。

代码语言:javascript
复制
import tkinter


from tkinter import Tk
root = Tk()

root.geometry("300x200+600+250")
root.configure(background="black")

Computation = ""
VarDisplay = Computation


def Display():
    global Computation
    VarDisplayTK.config(text=Computation)
    root.after(100, Display)
    
def Clear():
    global Computation
    Computation = ""
    print('clear !!!!!!!!!!!')
    
    
def Answer ():
    global Computation
    Computation = eval(Computation)
    tkinter.messagebox.showinfo("Answer",Computation)
    Computation = ""

def Trolling ():
    tkinter.messagebox.showinfo("Greetings", "Hello :)")

def OneFunc ():
    global Computation
    Computation += "1" 

def TwoFunc ():
    global Computation
    Computation += "2"
    
def ThreeFunc ():
    global Computation
    Computation += "3"
    
def FourFunc ():
    global Computation
    Computation += "4"
    
def FiveFunc ():
    global Computation
    Computation += "5"
    
def SixFunc ():
    global Computation
    Computation += "6"
    
def SevenFunc ():
    global Computation
    Computation += "7"
    
def EightFunc ():
    global Computation
    Computation += "8"
    
def NineFunc ():
    global Computation
    Computation += "9"
    
def ZeroFunc ():
    global Computation
    Computation += "0"
    
def AdditionFunc ():
    global Computation
    Computation += "+"
    
def MultiplicationFunc ():
    global Computation
    Computation += "*"
    
def SubtractionFunc ():
    global Computation
    Computation += "-"
    
def DivisionFunc ():
    global Computation
    Computation += "/"
    
def PlusMinusFunc ():
    global Computation
    Computation += "-"
    
def PercentageFunc ():
    global Computation
    Computation += "/100 *"
    
def DecimalFunc ():
    global Computation
    Computation += "."
    

test = tkinter.Label(root, text="Calc", width = 8).grid(column=0, row=0)
VarDisplayTK = tkinter.Label(root, text=VarDisplay, width = 20,bg='red',fg='White')
VarDisplayTK .grid(column=1,row=0,columnspan=3)
ACButton = tkinter.Button(root, text = "AC", width = 5, command = Clear)
ACButton.grid(column=0, row=1)
PlusMinusButton = tkinter.Button(root, text = "+ -",   width = 5, command = PlusMinusFunc)
PlusMinusButton.grid(column=1, row=1)
PercentageButton = tkinter.Button(root, text = "%",   width = 5, command = PercentageFunc)
PercentageButton.grid(column=2, row=1)
DivisionButton = tkinter.Button(root, text = "/",   width = 5, command = DivisionFunc)
DivisionButton.grid(column=3, row=1)
SevenButton = tkinter.Button(root, text = "7",   width = 5, command = SevenFunc)
SevenButton.grid(column=0, row=2)
EightButton = tkinter.Button(root, text = "8",   width = 5, command = EightFunc)
EightButton.grid(column=1, row=2)
NineButton = tkinter.Button(root, text = "9",   width = 5, command = NineFunc)
NineButton.grid(column=2, row=2)
MultiplicationButton = tkinter.Button(root, text = "*",  width = 5,  command = MultiplicationFunc)
MultiplicationButton.grid(column=3, row=2)
FourButton = tkinter.Button(root, text = "4",   width = 5, command = FourFunc)
FourButton.grid(column=0, row=3)
FiveButton = tkinter.Button(root, text = "5",   width = 5, command = FiveFunc)
FiveButton.grid(column=1, row=3)
SixButton = tkinter.Button(root, text = "6",   width = 5, command = SixFunc)
SixButton.grid(column=2, row=3)
MinusButton = tkinter.Button(root, text = "-",   width = 5, command = SubtractionFunc)
MinusButton.grid(column=3, row=3)
OneButton = tkinter.Button(root, text = "1",   width = 5, command = OneFunc)
OneButton.grid(column=0, row=4)
TwoButton = tkinter.Button(root, text = "2",   width = 5, command = TwoFunc)
TwoButton.grid(column=1, row=4)
ThreeButton = tkinter.Button(root, text = "3",   width = 5, command = ThreeFunc)
ThreeButton.grid(column=2, row=4)
AdditionButton = tkinter.Button(root, text = "+",   width = 5, command = AdditionFunc)
AdditionButton.grid(column=3, row=4)
ZeroButton = tkinter.Button(root, text = "0",   width = 5, command = ZeroFunc)
ZeroButton.grid(column=0, row=5)
DecimalButton = tkinter.Button(root, text = ".",   width = 5, command = DecimalFunc)
DecimalButton.grid(column=1, row=5)
EqualButton = tkinter.Button(root, text = "=",   width = 5, command = Answer)
EqualButton.grid(column=2, row=5)
RandomButton = tkinter.Button(root, text = ":)",    width = 5, command = Trolling)
RandomButton.grid(column=3, row=5)


Display()
# root.after(100, Display) # used more often than Display()
root.mainloop()

不确定这是建立一个Tkinter应用程序的正确方式。

请看这里为什么您的代码不能很好地工作:

Why is Tkinter widget stored as None? (AttributeError: 'NoneType' object ...)(TypeError: 'NoneType' object ...)

Why do my Tkinter widgets get stored as None?

https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name

引用:

每个Tkinter小部件的网格、pack和place方法都是就地操作的,并且始终不返回任何内容。这意味着您不能在创建小部件时在同一行调用它们。相反,应该在下面的线路上调用它们:

试图使您的代码更短的版本,仍然有效。

当您使用以0开头的字符串时,即03 +3

+-也不太好用。

代码语言:javascript
复制
import tkinter as tk


class App:

    def __init__(self, master):
        
        self.master = master
    
        self.keyz = ['AC', '+-','%','/','7','8','9','*','4','5','6','-','1','2','3','+','0','.','=',':)']
        
        self.master.geometry("270x210+600+250")
        self.master.configure(background="black")
        
        self.computation = tk.StringVar()
        
        self.computation.set("")
        
        self.computation.trace('w', self.callback)
     
        self.VarDisplay = tk.Label(self.master, text=self.computation.get(),
                                              height = 3, width = 20, bg = 'black', fg ='White')
        self.mbox = tk.messagebox
        
        self.draw()
        
        
    def callback(self, *args):
        
        # print('args : ', [*args])
        self.VarDisplay.config(text = self.computation.get())

    def draw(self):

        self.VarDisplay.grid(column=0,row=0,columnspan=3)
        
        col, row = 0 , 1
        for i in self.keyz:
            
            print(i, type(i), str(i))

            i = tk.Button(self.master, text = str(i), width = 5,  command = lambda i=i : self.comm(str(i)))
            
            i.grid(column = col , row = row)      
            
            print(i, type(i), str(i))
            
            
            if col >= 3:
                col = 0
                row += 1
                
            else: 
                col +=1
                
    def comm(self, string: str):
        
        if string == ':)':
            
            self.mbox.showinfo("Greetings", "Hello :)")
            
        elif string == 'AC':
            self.computation.set("")
            
        elif string == '=':
            
            self.mbox.showinfo("Answer", eval(self.computation.get()))
            self.computation.set("")
            
        elif string == '+-':
            
            
            if self.computation.get()[0] == '-':
                
                self.computation.set(self.computation.get()[1:])
            
            else:
                
                self.computation.set('-'+self.computation.get())
                
        
        else:
            self.computation.set(self.computation.get()+string)
        

if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    root.mainloop()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74029089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档