【问题标题】:How to make a condition that states when you click a button your input will show as messagebox如何创建一个条件,当您单击按钮时,您的输入将显示为消息框
【发布时间】:2022-06-29 07:00:19
【问题描述】:

我不知道如何在输入的地方以外打印计算器的答案。

我要插入的代码是:

Button(master, text=\"Submit\",width=23, height=3, command=lambda(?)).grid(row=5, column=0, columnspan=3)

? = 我想要定义的东西

我希望条目中的文本 self.e 打印在条目之外的消息框或标签中。

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

root = tk.Tk()

class sweltres:
      
    def clearall(self):
    
            self.e.delete(0,END)
 
    def clear1(self):
            self.txt=self.e.get()[:-1]
            self.e.delete(0,END)
            self.e.insert(0,self.txt)
 
    def action(self,argi):
     
            self.e.insert(END,argi)
 
    def __init__(self,master):
       
            master.title(\'Calculator\')
            master.geometry(\"100x50\")
            self.e = ttk.Entry(master)
            self.e.grid(row=0,column=0,columnspan=6,pady=3)
            self.e.focus_set() 
            
    
            Button(master,text=\'AC\',width=5,height=3,
                        fg=\"black\", bg=\"blue\",
            command=lambda:self.clearall()).grid(row=4, column=2)
 
            Button(master,text=\'C\',width=5,height=3,
                fg=\"red\",bg=\"blue\",
                command=lambda:self.clear1()).grid(row=4, column=0)
 
            Button(master,text=\"7\",width=5,height=3,
                fg=\"white\",bg=\"blue\",
                command=lambda:self.action(\'7\')).grid(row=1, column=0)
 
            Button(master,text=\"8\",width=5,height=3,
                fg=\"white\",bg=\"blue\",
                command=lambda:self.action(8)).grid(row=1, column=1)
 
            Button(master,text=\"9\",width=5,height=3,
                fg=\"white\",bg=\"blue\",
                command=lambda:self.action(9)).grid(row=1, column=2)
 
            Button(master,text=\"4\",width=5,height=3,
                fg=\"white\",bg=\"blue\",
                command=lambda:self.action(4)).grid(row=2, column=0)
 
            Button(master,text=\"5\",width=5,height=3,
                fg=\"white\",bg=\"blue\",
                command=lambda:self.action(5)).grid(row=2, column=1)
 
            Button(master,text=\"6\",width=5,height=3,
                fg=\"white\",bg=\"blue\",
                command=lambda:self.action(6)).grid(row=2, column=2)
 
            Button(master,text=\"1\",width=5,height=3,
                fg=\"white\",bg=\"blue\",
                command=lambda:self.action(1)).grid(row=3, column=0)
 
            Button(master,text=\"2\",width=5,height=3,
                fg=\"white\",bg=\"blue\",
                command=lambda:self.action(2)).grid(row=3, column=1)
 
            Button(master,text=\"3\",width=5,height=3,
                fg=\"white\",bg=\"blue\",
                command=lambda:self.action(3)).grid(row=3, column=2)
   
            Button(master,text=\"0\",width=5,height=3,
                fg=\"white\",bg=\"blue\",
                command=lambda:self.action(0)).grid(row=4, column=1)
                        
            Button(master, text=\"Exit wtf\",width=23,height=3, 
                 fg=\"white\",bg=\"red\", command=master.destroy).grid(row=6, column=0, columnspan=3)


sweltres(root)
root.mainloop()
  • 我不确定你想要什么。你能定义一个你想要的函数吗?
  • 就像如果我单击按钮 12345 然后单击提交按钮,则会弹出一个 msgbox 或文本,并说类似“您选择的数字是 12345”

标签: python python-3.x tkinter tkinter-entry tkinter-button


【解决方案1】:

是的,您可以弹出一个消息框,其文本是Entry 中的文本:

class sweltres:
    def msg(self):
            tk.messagebox.showinfo(title='Submit Button',message = self.e.get())

    def __init__(self,master):
        # your existing parts elided
        tk.Button(master, text="Submit",width=23, height=3, command=self.msg).grid(row=5, column=0, columnspan=3)

【讨论】:

    【解决方案2】:

    对不起,如果我没有很好地理解你的答案,但如果我理解了,我以前做过这种事情。您需要将条目中的文本self.e 打印到标签或其他东西上。您可以执行以下操作:

    def callback(event):
      text = tk.StringVar()
      text.set(self.e.get())
      lbl = ttk.Label(master, textvariable=text)
      # lbl.whatever_geometry_manager_you_use(**options)
      lbl.pack(side="bottom")
    

    对于消息框,请在顶部执行此操作:

    from tkinter.messagebox import showinfo
    
    def callback(event):
      text = tk.StringVar()
      text.set(self.e.get())
      showinfo(title="Result", message=text)
    

    现在,您可以按如下方式定义您的按钮:

    # submit_btn being your variable name
    submit_btn = Button(master, text="Submit", width=23, height=3, command=callback).grid(row=5, column=0, columnspan=3)
    

    或者,如果您喜欢并喜欢lambda

    # submit_btn being your variable name
    submit_btn = Button(master, text="Submit", width=23, height=3, command=lambda: callback()).grid(row=5, column=0, columnspan=3)
    

    毫无疑问,有更好的方法来做到这一点。此答案部分是对您问题的回答,部分是对可以回答您问题的其他人的澄清。

    【讨论】:

      猜你喜欢
      • 2021-05-02
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多