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()
Related
My program needs to track the user input at all times. For that i think the input() command is the easiest solution. The problem is: At the same time there should be a Tkinter GUI in fullscreen mode running. I've tried a few things, but nothing really worked out. Here is a simplified program, which shows the problems:
from tkinter import *
import tkinter as tk
from threading import Thread
def GUI():
root=tk.Tk()
Text = Label(master=root, text="Test").pack(side="top")
Button = Button(master=root, text="Button").pack(side="top")
root.mainloop()
def input_loop():
x = input()
print(x)
t1 = Thread(target=GUI)
t2 = Thread(target=input_loop)
t1.start()
t2.start()
Even though now both loops work, i still can't type into the console unless I manually select the console window. Other solutions like the entry widget in tkinter won't work because there is no place for them in my actual program. Please let me know if you find something which works reliably.
i didn't get your problem clearly but i will try to help .
so you are saying the program running on full screen but you want to run it on some resolution?
from tkinter import *
import tkinter as tk
from threading import Thread
def GUI():
root=tk.Tk()
# Program title
root.title("Title")
# Preventing user from fullscreen/resize the window
root.resizable(0, 0)
# Resolution of the windows in pixles
root.geometry(f"{820}x{460}")
Text = Label(master=root, text="Test").pack(side="top")
Btn = Button(master=root, text="Button").pack(side="top")
root.mainloop()
def input_loop():
x = input()
print(x)
t1 = Thread(target=GUI)
t2 = Thread(target=input_loop)
t1.start()
t2.start()
also i changed button function to btn because i faced an error with it , and you can now complete the program and play with the resolution of the buttons and windows so on .
this might be a noob question but I have a simple script to monitor the current in my area which I use customtkinter as my GUI.
The script have two buttons which are Start and Restart. The Start button is to start the program and the Restart in to simply to Restart the whole code again which you could see on the snippet of the code below.
import sys
import os
#from tkinter import Tk, Label, Button
import customtkinter
def restart_program():
"""Restarts the current program."""
root.destroy()
os.startfile("testbed.py")
root = customtkinter.CTk()
#Label(root, text="Hello World!").pack()
#Button(root, text="Restart", command=restart_program).pack()
label = customtkinter.CTkLabel(root, text="Hello World!").pack()
button = customtkinter.CTkButton(root, text="Restart", command=restart_program).pack()
root.mainloop()
but when I clicked on the restart button it does not execute the os.startfile("testbed.py") part is there something wrong with my logic?
So im very new to python and I'm basically trying to make a program were the user inputs a targets IP and then presses a button below to Execute the command (start the pinging process) and this I've done successfully but now I wanted to add an exit button below to stop the pinging process but I've got a problem. The button works fine before you press execute (works as intended it closes the application window) but when the script is running the button doesn't work at all and just kinda crashes the window but keeps the command running. How can I make the button work att all times (make it work when the script is running).
import tkinter as tk #import tkinter and tkinter library
from tkinter import ttk #package to style widget
import subprocess
def window(): #runs the window ??? i think
#address = input("Enter Ip Here:")
subprocess.call(f"ping {text.get()} -t -l 65500")
# root window
root = tk.Tk() # root?
root.geometry('200x200') #window size
root.resizable(False, False) # makes window non rezisable
root.title('Ping Of Death') #window title
var = tk.StringVar #variable tkinter = StringVar
text = tk.Entry(root,textvariable=var, width = 25)
text.pack()
# execute button
execute_button = ttk.Button( #defines the execute button
root,
text='Execute', #defines the text on the button
command=window
)
execute_button.pack( #package for button
ipadx=5, #x size
ipady=5, #y size
expand=True # ???
)
exit_button = ttk.Button(
root, text="Exit" ,
command=root.quit
)
exit_button.pack(pady=20)
root.mainloop() #to keep everything in a loop
I'm pretty sure that subprocess.call is blocking, so search on "subprocess.call non-blocking" and asyncio as an alternative.
I created a tkinter Toplevel window for my application and later in the program destroyed it but after destroying the window the program doesnt get executed further and get struck there itself doing nothing . Here is the code that I used :-
#login.py
from tkinter import *
class gui:
def __init__(self):
#does something
def login(self):
self.winLogin.destroy()
def guilogin(self):
self.winLogin = Toplevel()
btn = Button(self.winLogin,command=self.login,text='asd')
btn.pack()
self.winLogin.mainloop()
#main.py
import login
from tkinter import *
main = Tk()
a = login.gui()
a.guilogin()
if True:
#some code and this part doesnot get executed
main.mainloop()
else:
main.destroy()
I run main.py file and the code get struck and do nothing before the if part . I tottaly have no idea whats wrong . Pls. Help!
As furas said in the comments, you should not call mainloop on the toplevel, instead use grab_set to disable the main window and wait_window to wait for the toplevel to be closed:
from tkinter import Tk, Toplevel, Button
def login():
top = Toplevel(root)
Button(top, text="Quit", command=top.destroy).pack()
top.grab_set() # deactivate the main GUI while top is opened
root.wait_window(top) # wait for top to be closed before doing the rest
print("logged in")
root = Tk()
Button(root, text="login", command=login).pack()
root.mainloop()
So I have this code:
try:
# for Python2
from Tkinter import *
except ImportError:
# for Python3
from tkinter import *
class Injector():
def __openInjector(self):
root = Tk()
root.geometry('600x400')
root.title('Toontown Rewritten Injector')
root.resizable(False, False)
def __init__(self):
self.code = ''
self.__openInjector()
def runInjectorCode(self):
exec(self.code.get(1.0, 'end'), globals())
def __openInjector(self):
root = Tk()
root.geometry('600x400')
root.title('Toontown Rewritten Injector')
root.resizable(False, False)
frame = Frame(root)
self.code = Text(frame, width=70, height=20)
self.code.pack(side='left')
Button(root, text='Inject!', command=self.runInjectorCode).pack()
scroll = Scrollbar(frame)
scroll.pack(fill='y', side='right')
scroll.config(command=self.code.yview)
self.code.config(yscrollcommand=scroll.set)
frame.pack(fill='y')
Injector()
In the IDLE console it works fine and does everthing I want it to do. But whenever I run the .py file on my Desktop. The black window appears, then just closes and nothing happens. Any help?
First, you have two methods in your class with the same name. The first one gets overwritten by the second one. At the end of that second one, you need the following line:
root.mainloop()
This will actually run the GUI. It's needed when running from a script, but not when running within the interactive interpreter.
Add it at the end of the second __openInjector:
...
self.code.config(yscrollcommand=scroll.set)
frame.pack(fill='y')
root.mainloop()
At the end of your second __openInjector method, add the line: root.mainloop().
This is necessary for Tkinter to run your code. mainloop is really nothing more than an infinite loop that waits for events. An event may be a user interaction, such as clicking a button.
My guess is you don't need mainloop when running interactively for purely convenience reasons.