Tkinter only prints empty strings - python

I am trying to create a GUI with tkinter to print a string entered in the entry box. I have written the following code
import tkinter as tk
top= tk.Tk()
labelAmount=tk.Label(text='Amount')
Amount= tk.Entry()
amount=Amount.get()
Add = tk.Button(top, text='Add', command = lambda : print(amount))
Add.pack()
Amount.pack()
labelAmount.pack()
top.mainloop()
The problem is that when I am pressing the 'Add' button, an empty string is being printed.
It would be great if someone can help me with this problem.

Related

Alternative Process in Returning a User Input from a Tkinter Entry

Is there Anyway to Bind the Key "Enter" to the button of a tkinter entry? For example I was to query a value from a database and entered the Value to the textbox, but i have to press the button on the screen rather than pressing the enter button on the keyboard. Is there anyway to do it?
Solution
For binding the "enter" key, just use the win.bind('<Return>', command). Here is an example for your case (using label):
#Import the tkinter library
from tkinter import *
#Create an instance of tkinter Tk
win = Tk()
#Set the geometry
win.geometry("650x250")
#Event Handler function
def handler(e):
label= Label(win, text= "You Pressed Enter")
label.pack()
#Create a Label
Label(win, text= "Press Enter on the Keyboard", font= ('Helvetica bold', 14)).pack(pady=20)
#Bind the Enter Key to Call an event
win.bind('<Return>', handler)
win.mainloop()
Other Key Bindings
Most keys have their own name as the name used while binding. But some keys have different key-binds. A list of key-binds and events in tkinter can be found here (These can be used using the tkinter function tkinter.bind('<bind-code>', handler))

Why my button in tkinter don't works normal [duplicate]

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed last year.
I wanted to make button in tkinter, but when I started program, the command always calls when code just starts.
Here is example code:
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.title("Why this don't works???")
window.wm_geometry("100x100")
def message():
messagebox.showinfo("Hi there")
button = tk.Button(text="Hello", command=message())
button.grid(column=0, row=0)
while True:
window.update()
And then, button didn't worked. (When you press it, it don't works.)
I don't know what I'm doing wrong, so I need help.
The command should be a pointer to a function
In the code you wrote, the command gets the return value from the function.
command=message()
The correct way is
command = message
The problem is you are requesting a return value from the fucnction. Try using this.
from tkinter import *
# import messagebox from tkinter module
import tkinter.messagebox
# create a tkinter root window
root = tkinter.Tk()
# root window title and dimension
root.title("When you press a button the message will pop up")
root.geometry('75x50')
# Create a messagebox showinfo
def onClick():
tkinter.messagebox.showinfo("Hello World!.", "Hi I'm your message")
# Create a Button
button = Button(root, text="Click Me", command=onClick, height=5, width=10)
# Set the position of button on the top of window.
button.pack(side='top')
root.mainloop()
You have 2 errors:
first:
It must be command=message
second:
You must give a message argument too, you entered a title only.
Or, what you can do is.
Add another variable.
command = message()
Before this line,
button = tk.Button(text="Hello", command=message())
And chande this line to,
button = tk.Button(text="Hello", command=command)

tkinter: Copy to clipboard via button

The idea of the code is to create N amount of buttons that copy text to the clipboard when pressed, overwriting and saving the text from the last pressed button.
from tkinter import *
import tkinter
r = Tk()
age = '''
O.o
giga
'''
gage = 'vrum'
r.title("getherefast")
def gtc(dtxt):
r.withdraw()
r.clipboard_clear()
r.clipboard_append(dtxt)
r.update()
tkinter.Button(text='age', command=gtc(age)).grid(column=1, row=0)
tkinter.Button(text='gage', command=gtc(gage)).grid(column=2, row=0)
r.mainloop()
With this code I expected to get 2 buttons 'age' and 'gage' and when I press them to get respectively the value saved in the var.
The problem is that the tkinter UI does not load and the Idle window is just standing open.
The result is that I get 'vrum' copied to the clipboard (If age button is the only 1 present I get the correct value but still no GUI from tkinter).
As additional information I'm writing and testing the code in IDLE, Python 3.10.
The problem is that the tkinter UI does not load
Yes it does, but you told it to withdraw(), so you don't see it.
To do this you need a partial or lambda function, you can't use a normal function call in a command argument. Try this:
import tkinter
r = tkinter.Tk()
age = '''
O.o
giga
'''
gage = 'vrum'
r.title("getherefast")
def gtc(dtxt):
r.clipboard_clear()
r.clipboard_append(dtxt)
tkinter.Button(text='age', command=lambda: gtc(age)).grid(column=1, row=0)
tkinter.Button(text='gage', command=lambda: gtc(gage)).grid(column=2, row=0)
r.mainloop()

How to create a tkinter error message box [duplicate]

This question already has answers here:
How to create a message box with tkinter?
(5 answers)
Closed 5 years ago.
I am wanting to create a simple message box in Tkinter that appears and displays the exact error message. Could anyone direct me to how this might be
achieved in tkinter, I have not been able to find much on this topic.
E.g:
traceback.format_exc().replace(':', '-')
ctypes.windll.user32.MessageBoxW(0, "Error", "Have you checked your fridge?"d, 1)
^
#'SyntaxError: invalid syntax'
I am wanting to add this with pyinstaller. I suppose pyinstaller creates a text file and you can see in cmd before it closes, but it would be nice if a message box appear with exact traceerror.
from tkinter import messagebox
messagebox.showerror("Title", "Message")
check here for more info
This login system which will pop up messagebox when you provide wrong data for entry messagebox should be entered into the entry if not the messagebox will pop up prompting you an error as occured
from tkinter import *
from tkinter import messagebox
def top():
if entry1.get() == "messagebox":
log.destroy()
root.deiconify()
else:
messagebox.showerror("error", "try again")
messagebox.showinfo("my message","this is an example of showinfo\nmessagebox")
messagebox.showwarning("warning", "show warning example in tkinter" )
root = Tk()
root.geometry("400x400")
log = Toplevel(root)
log.geometry("200x200")
label1 = Label(log, text="password")
entry1 = Entry(log)
button1 = Button(log, text="login", command=top)
label1.pack()
entry1.pack()
button1.pack(side="bottom")
lab = Label(root, text="welcome bro").pack()
root.withdraw()
root.mainloop()

How do I highlight (select) text in Text widget with a button click?

I'm using Tkinter 8.5 and Python 3.3, and I would like my users to be able to copy the text in a Text widget on the click of a button. I've got that part working, but I also wanted to display this visually to the user by highlighting (selecting) the text as well.
Here is some sample code:
from tkinter import *
def copy():
root.clipboard_clear()
root.clipboard_append(entry.get(0.0, END))
entry.select_all() # though I wish it did, this attribute doesn't exist!
root = Tk()
entry = Text(root)
entry.pack()
button = Button(root, text="Copy your text", command=copy)
button.pack()
Is there a simple way to do this?
Try
entry.tag_add('sel', '1.0', 'end')
or
entry.tag_add('sel', '1.0', 'end-1c')

Categories