Home > other >  How to make a condition that states when you click a button your input will show as msgbox
How to make a condition that states when you click a button your input will show as msgbox

Time:06-27

Help me please I'm new to this and I don't know how to execute the thing that I want to be executed

The code that I want to insert is

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

? = The thing that i want to be define

You can do msgbox or label as long the input can be printed inside and outside of the Entry box

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('Calulator')
            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()

CodePudding user response:

Sorry if i didn't understand your answer very well, but if I did, I did this kind of thing before. You need to print the text in your entry, self.e, to a label or something. You can do as follows:

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")

Now, you can define your button as follows:

# 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)

Or if you prefer and like 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)

There are no doubt better ways to do this. This answer is partly an answer to your question and partly a clarification to others who can answer your question.

CodePudding user response:

Yes, you can get a messagebox to pop up with its text being the text in the 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)

  • Related