Why is exit command is not quitting sometimes in python tkinter exe? - python

In some os's, When you make a tkinter window and put it to mainloop, and make a button for exit and convert it to exe (with pyinstaller) like this:
from tkinter import *
def exit_():
window.destroy()
exit()
window = Tk()
text = Label(window, text = "Hello World")
text.pack()
window.protocol('WM_DELETE_WINDOW', exit_)
window.mainloop()
If you use the built-in exit() command, then sometimes the window will not get closed.
Is there an Answer for that?

Yes there is an answer for it.
Don't use exit() in tkinter exe.
For that use sys.exit() the built-in module in python.
The full code:
from tkinter import *
from sys import exit as sexit
def exit_():
window.destroy()
sexit()
window = Tk()
text = Label(window, text = "Hello World")
text.pack()
window.protocol('WM_DELETE_WINDOW', exit_)
window.mainloop()

Related

Tkinter program won't stop running properly after calling PySimpleGUI function

my program won't stop running after clicking Close on the window. It's always showing "You program is still running! Do you want to kill it?" when try to close IDLE. But it works fine when comment out the sg.popup("sg Popup") line. Please help!
from tkinter import *
import PySimpleGUI as sg
sg.popup("sg Popup") #comment this line then works fine
root = Tk()
root.title("Main Menu")
root.geometry("320x240")
a = Label(root, text ="Hello World!!", font=16)
a.pack()
root.mainloop()

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)

how can i open multiple windows in tkinter in another file?

i have a problem in my code in python (tkinter)
I have two files (F1.py) and (F2.py), each of which is a window, and I have another file called (main.py) that opens a window with two buttons, I want to click one of the (F1) files every time Or (F2) open.
What should I do ?
this is my code:
from tkinter import *
main = Tk()
def f1():
import f1.py
# I do not know what to do to run the file f1.py here !!
def f2():
import f2.py
# I do not know what to do to run the file f2.py here !!
btn_f2 = Button(main,text="open f2",command=f2).pack()
btn_f1 = Button(main,text="open f1",command=f1).pack()
mainloop()
Here is the work around according to your need, Since you want to execute your other tkinter window from your main python script. Here is the way to do that.
in your main.py
from tkinter import *
import os
import sys
import f1,f2
main= Tk()
def open(filename):
os.chdir("D:\\PYTHON_FILES\\") #change this path to your path where your f1.py and f2.py is located
# print("current dir "+os.getcwd())
os.system('python '+filename) #runnning the python command on cmd to execute both windows
btn_f2 = Button(main,text="open f2",command=lambda: open("f1.py")).pack()
btn_f1 = Button(main,text="open f1",command=lambda: open("f2.py")).pack()
main.mainloop()
Now define f1.py for example
from tkinter import *
window= Tk()
window.title('f1 Hello Python')
window.geometry("300x200+10+10")
mainloop()
Similarly for example we have f2.py
from tkinter import *
window= Tk()
window.title('f2 Hello Python')
window.geometry("300x200+10+10")
mainloop()
Now If you want to go for the legit way using TopLevel in Tinkter as #Matiis Described then here is clean and perfect way to achieve the goal.
Simply make a main.py as below
from tkinter import *
root = Tk()
root.geometry("200x200")
#here define your f1 window
def f1():
top = Toplevel(root)
top.geometry("400x400")
top.title("I am f1 window smaller than f2 but bigger than root")
top.mainloop()
#Similarly here define your f2 window
def f2():
top = Toplevel(root)
top.geometry("500x500")
top.title("I am f2 window bigger than f1")
top.mainloop()
btn1 = Button(root, text = "open f1", command = f1)
btn2 = Button(root, text = "open f2", command = f2)
btn1.place(x=75,y=50)
btn2.place(x=75,y=20)
root.mainloop()
I might be a little late with this, but I've recently started learning python and I had the same issue with tkinter. I had a "main.py" file and I wanted it to open "f1.py" by clicking a button.
I realized that when you configure a button in tkinter you have to assign a function so I wrote "main.py" as a regular pyhon/tkinter file importing f1.py:
from tkinter import *
from tkinter import ttk
import f1
main = Tk()
main.title("Main window")
but = Button(main, text="Open f1.py", command= f1.f1_func)
but.pack()
main.mainloop()
And instead of defining the function on the main file (def but_openwindow(): etc...) I wrote that function on the "f1.py" but JUST as a function, not as a mainloop. So the f1.py would be something like this:
from tkinter import *
from tkinter import ttk
def f1_func():
other = Toplevel()
other.title("Other window")
text = Label(other, text="This is another window")
text.pack()
And you could repeat it for any number of other .py files. So far I found out 2 important things:
1- I thought it wouldn't be necessary, but apparently you have to start f1.py importing tkinter libraries, otherwise it wouldn"t work. At least for me it didn't.
2- The files that you want to import can't have a "mainloop" or it just opens the other window as soon as you execute the main file.
I have yet to try to make an .exe of the main file, but I'm hoping to get there.
Sorry again for being late, hope it helps, if not for you, at leas for anyone else that might encounter this same problem.

Printed text should appear in separate window - How?

this question will be relatively simple, just do not get anywhere. It's more of a basic question.
When I write
print ("hello world")
it appears in the script program in the message box bellow. But now I want it to open in a separate fesnter, created with tkinter. I wrote it down like this (see picture or code), but I know that print itself must not be in brackets, how can I solve this problem?
from tkinter import *
a = ("Hello World")
root = Tk()
T = Text(root, height=50, width=150)
T.pack() T.insert(END, print a)
mainloop()
The "hello world" must appear in a separately-opened window.
I am happy about any answer.
Thanks in advance
PyBeginner
I didn't understand your question but can you try this one if it helps .
from tkinter import *
root = Tk()
a = Label(root, text ="Hello World")
a.pack()
root.mainloop()
Resource: https://www.geeksforgeeks.org/python-tkinter-messagebox-widget/ Also, if you want to you can delete showinfo code and just run the w.
from tkinter import *
from tkinter import messagebox
root = Tk()
root.geometry("400x300")
w = Label(root, text='Hello World!', font="50")
w.pack()
messagebox.showinfo("Hello World", "This is a Hello World message!!!")
root.mainloop()

Sounds in Python (tkinter) [duplicate]

I am building a program for Windows PCs that contains a lot of buttons and seems very plain. So I was wondering, can I make it so when you push a button (using tkinter), can I play a sound to liven up the program a bit? Please keep in mind I am learning so please dumb it down a bit.
Assuming your file is a WAV:
from tkinter import *
from winsound import *
root = Tk() # create tkinter window
play = lambda: PlaySound('Sound.wav', SND_FILENAME)
button = Button(root, text = 'Play', command = play)
button.pack()
root.mainloop()
Assuming your file is a MP3:
from Tkinter import *
import mp3play
root = Tk() # create tkinter window
f = mp3play.load('Sound.mp3'); play = lambda: f.play()
button = Button(root, text = 'Play', command = play)
button.pack()
root.mainloop()
You might want to consider using pygame as a cross-platform alternative to winsound.
import tkinter as tk
from pygame import mixer
mixer.init()
sound = mixer.Sound("sound.ogg")
root = tk.Tk()
tk.Button(root, command=sound.play).pack()
root.mainloop()
Refer to the docs for more information.
You first need to link the click of your mouse on the image, with an even handler, then simply define an on_click function:
def on_click(event):
winsound.Beep('frequency', 'duration')
Here you can find more information about playing sounds in python.
Just use
import os
os.system("play sound.mp3")

Categories