Python Tkinter pop-up not being displayed - python

I am trying to get a Tkinter popup to display when a button is clicked.My issue is that every thing runs just fine except the popup will not produce. I have tried multiple ways to create the popup using tkMessagebox and Toplevel() but still not luck. The program runs but when the button is click nothing happens. I have referenced similar post but still can not find the issue in my code. Any thoughts?
from tkinter import *
def new():
root2 = Tk()
root2.geometry('250x250')
l = Label(root2,text="Please Scan Tag").pack()
root2.mainloop()
# setting main frame
root = Tk()
root.geometry('800x650')
root.title("Pass")
root.configure(background= "white")
label_0 = Label(root, text="Pass",width=10,font=("bold", 50),fg= "green",bg="white")
label_0.place(x=186,y=76)
Button(root,command="new", text='new',font=
("bold",15),width=15,height=4,bg='blue',fg='white').place(x=155,y=300)
root.mainloop()

The command option requires a reference to a callable function, not a string.
Button(root,command=new, ...)

Related

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

Button.wait_variable usage in Python/Tkinter

There have already been several topics on Python/Tkinter, but I did not find an answer in them for the issue described below.
The two Python scripts below are reduced to the bare essentials to keep it simple. The first one is a simple Tkinter window with a button, and the script needs to wait till the button is clicked:
from tkinter import *
windowItem1 = Tk()
windowItem1.title("Item1")
WaitState = IntVar()
def submit():
WaitState.set(1)
print("submitted")
button = Button(windowItem1, text="Submit", command=submit)
button.grid(column=0, row=1)
print("waiting...")
button.wait_variable(WaitState)
print("done waiting.")
windowItem1.mainloop()
This works fine, and we see the printout “done waiting” when the button is clicked.
The second script adds one level: we first have a menu window, and when clicking the select button of the first presented item, we have a new window opening with the same as above. However, when clicking the submit button, I don’t get the “Done waiting”. I’m stuck on the wait_variable.
from tkinter import *
windowMenu = Tk()
windowMenu.title("Menu")
def SelectItem1():
windowItem1 = Tk()
windowItem1.title("Item1")
WaitState = IntVar()
def submit():
WaitState.set(1)
print("submitted")
button = Button(windowItem1, text="Submit", command=submit)
button.grid(column=0, row=1)
print("waiting...")
button.wait_variable(WaitState)
print("done waiting")
lblItem1 = Label(windowMenu, text="Item 1 : ")
lblItem1.grid(column=0, row=0)
btnItem1 = Button(windowMenu, text="Select", command=SelectItem1)
btnItem1.grid(column=1, row=0)
windowMenu.mainloop()
Can you explain it?
Inside your SelectItem1 function, you do windowItem1 = Tk(). You shouldn't use Tk() to initialize multiple windows in your application, the way to think about Tk() is that it creates a specialized tkinter.Toplevel window that is considered to be the main window of your entire application. Creating multiple windows using Tk() means multiple main windows, and each one would need its own mainloop() invokation, which is... yikes.
Try this instead:
windowItem1 = Toplevel()

fix a delay issue when switching from one window to another using TKinter?

I'm developing a GUI using TKinter for a kiosk based application and I've developed a basic code which switches from one window to another on button click.
Below is the code I've tried and it runs successfully. However I'm finding some delay issues when switching from one window to another. When user presses the button on the root window, there is sufficient amount of delay occurred before second screen display. I've tested it step by step and realised that the way image opened and displayed, it takes sufficient processing time and that creates a delay issues.
try:
from Tkinter import *
import Tkinter as tk
except ImportError:
from tkinter import *
import tkinter as tk
from functools import partial
from PIL import ImageTk, Image
def test(root):
root.withdraw()
master1 = tk.Toplevel()
coverPhoto = Image.open('/home/pi/x.jpg')
coverPhoto = ImageTk.PhotoImage(coverPhoto)
lblBackground = Label(master1, width=ws, height=hs, image=coverPhoto)
lblBackground.photo = coverPhoto
lblBackground.pack(fill=BOTH, expand=YES)
master1.config(width=ws, height=hs)
master1.attributes('-fullscreen', True)
master1.mainloop()
return
root = tk.Tk()
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
root.title(' Sample test ')
idPhoto = Image.open('/home/pi/x.jpg')
idPhoto = ImageTk.PhotoImage(idPhoto)
lblImg = Label(root, width=ws, height=hs, image=idPhoto)
lblImg.photo = idPhoto
lblImg.pack(fill=BOTH, expand=YES)
startImg = Image.open('/home/pi/y.jpg' )
startImg = ImageTk.PhotoImage(startImg)
button = tk.Button(lblImg, image=startImg, highlightthickness=1, bd=0, command=lambda : test(root))
button.image = startImg
button.place(x=0, y=hs - 120, width=ws, height=120)
root.attributes('-fullscreen', True)
root.config(width=ws, height=hs)
root.mainloop()
Is there any other calling way which will take less time in processing? I want a smooth go when changing from one screen to another?
You could load all images before you start the mainloop.
That would of course increase the startup time of the application, but it should reduce window switch times.
Edit: Also place all the ImageTk.PhotoImage calls after root = tk.Tk() but before the mainloop. That is, take the code for coverPhoto out of test, and put it between root and mainloop.
Edit2: A white flash when a window closes is probably caused by the relatively slow graphics hardware on the raspberry pi, combined with how X11 works.
Instead of two top-pevel windows, try using a tkinter.ttk.Notebook with two "tabs". Hide and unhide the relevant tabs in the same X11 window.

"execfile" doesn't work properly

I'm trying to make a launcher for my Python program with Tkinter. I used the execfile function, and fortunately it opened the target GUI. However, none of the buttons would work, and it would say the global variable most functions reference isn't defined.
The code to launch the program:
def launch():
execfile("gui.py")
That works. The base code for the target program:
from Tkinter import *
gui = Tk()
gui.title("This is a GUI")
EDIT:
Example of a button:
def buttonWin():
buttonWindow = Toplevel(gui)
button = Button(buttonWindow, text = "Button", width = 10, command = None)
button.pack()
When it references that 'gui' variable for Toplevel, it comes up with an error. I tried defining the 'gui' variable in the Launcher script, but that only caused the target script to open first, instead of the Launcher:
gui = Tk()
launcher = Tk()
launcher.title("Launcher")
def launch():
return execfile("gui.py")
launchButton = Button(launcher, text = "Launch", width = 10, command = launch)
When I try pressing one of this program's buttons, I get a NameError:
$NameError: Global variable 'gui' is not defined$
Also this is in Python 2.7.5.
Thank you anyone who answers, and sorry for any errors with the code blocks; I'm new.
The problem is that you have structured the Tkinter program incorrectly.
In "gui.py" you should have something like:
from Tkinter import *
gui= Tk()
gui.mainloop()
You can add buttons to perform functions and customize it:
from Tkinter import *
gui = Tk()
gui.title("This is a GUI")
def launch():
execfile("gui.py")
launchbutton = Button(gui, text='Launch Program', command=launch)
launchbutton.pack()
gui.mainloop()
I think with your function buttonWin you were trying to do what is normally handled by a class; see unutbu's answer here.
I'm not sure if I've addressed your problem, but this should be a start.

Categories