Tkinter window not responding when push buttton - python

I have a program that I wrote in tkinter, but if the while true loop starts running, the window stops working(not responding) what should I do?
code:
import pyautogui
import sys
import keyboard
def basla():
while True:
if keyboard.is_pressed("shift"):
pyautogui.press("enter")
pyautogui.typewrite("/pull ")
elif keyboard.is_pressed("E"):
pyautogui.press("enter")
pyautogui.typewrite("/me WHO GAS")
pyautogui.press("enter")
def bitir():
sys.exit()
pencere = Tk()
pencere.title("Keybinder")
pencere.geometry("300x300+10+10");
baslat_buton = Button(pencere)
baslat_buton.config(text="Başlat", bg="black",width=8,height=2, fg="white", command=basla)
baslat_buton.place(x=100,y=100)
bitir_buton = Button(pencere)
bitir_buton.config(text="Bitir", bg="black", fg="white",width=8,height=2, command=bitir)
bitir_buton.place(x=100,y=150)
mainloop()```

"In tkinter don't use while True which runs forever (or runs longer time) because it blocks mainloop() in tkinter and it can't update items in window, and it looks like it freezes."
Check out this topic and see if it makes it clear to you. tkinter root.mainloop with While True loop

Related

Always ask for user input while tkinter gui is running in fullscreen mode

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 .

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

python tkinter: Calling a objects method on button click does not work

here is my sample code:
from time import sleep
import tkinter as tk
import threading
class Action:
counter = 0
def do_something(self):
while True:
print('Looping')
sleep(5)
action = Action()
root = tk.Tk()
button = tk.Button(root, text='pressme harder', command=threading.Thread(target=action.do_something()).start())
button.grid(row=1, column=0)
root.mainloop()
What am I expecting?
I'm expecting that as soon as I click the button in the UI an new thread is running, which is looping in the background and does not interfere with the UI (or later maybe other threads doing tasks in the background)
What is really happening?
When running the code, the method of the class is executeds immdiately and locking the procedure. root.mainloop() is never reached and therefore no UI is drawn
Alternatively I tried the following change:
button = tk.Button(root, text='pressme harder', command=threading.Thread(target=lambda: action.do_something()).start())
This behaves in the following (imho wrong) way:
The method is also called immediately, without pressing the button. This time the UI is drawn but seems the be locked by the thread (UI is slow/stuttering, pressing the buttom does not work most of the time)
Any Idea whats wrong there? Or how do I handle this in a more stable way?
You shouldn't try to start a thread directly in the button command. I suggest you create another function that launches the thread.
from time import sleep
import tkinter as tk
import threading
class Action:
counter = 0
def do_something(self):
while True:
print('Looping')
sleep(2)
print("Finished looping")
def start_thread(self):
thread = threading.Thread(target=self.do_something, daemon=True)
thread.start()
action = Action()
root = tk.Tk()
button = tk.Button(root, text='pressme harder', command=action.start_thread)
button.grid(row=1, column=0)
root.mainloop()

Tkinter isn't respoding while loop is running! PYTHON

So I was just programming basic spam program that I could come up with in python.
However when I press "Spam" button in the Tkinter window,It wont respond until function is ended :(!
Here is the code :
import pyautogui
import time
from tkinter import *
import keyboard
root = Tk()
root.geometry("300x250")
def retrive_input():
inputValue = textBox.get("1.0","end-1c")
spams(inputValue)
print(inputValue)
textBox = Text(root,height=5,width=30)
textBox.grid(row=1)
buttonCommit = Button(root,height=1,width=10,text="Spam
Word!",command=lambda:retrive_input())
buttonCommit.grid(row=2)
def spams(wordtospam):
x_cor = 962
y_cor=541
pyautogui.click(962,541)
for i in range(9999):
pyautogui.press("enter")
pyautogui.typewrite(wordtospam)
pyautogui.press("enter")
time.sleep(10)
pyautogui.press("enter")
pyautogui.typewrite('random stuff')
pyautogui.press("enter")
time.sleep(8)
upali = True
#spams('')
root.mainloop()
I also want to add a keydown checker, so if I press (for example) f5, program will stop working until I press f4.
Thanks ahead!
(This is in game program that will spam me something :), so to type in game u press enter, typewrite, enter and it sends )

Python Functions execute in wrong order, why?

When I run the program, it first waits until I enter input, then it will play the sound, then it shows the window with my image. Why is it doing this out of order?
from tkinter import *
import winsound
main = Tk()
main.state('zoomed')
main.geometry("1366x768")
# Displays a gif.
def show():
dollar_canvas = Canvas(width=50, height=50, bg='lightgrey', highlightthickness=0)
dollar_canvas.place(x=850, y=25)
my_gif = PhotoImage(file='Dollar50x50.gif')
dollar_canvas.image = my_gif
dollar_canvas.create_image(0, 0, image=my_gif, anchor=NW)
# Accepts an input, such as enter.
def getinput():
a = input()
# Plays a ring sound.
def play():
winsound.PlaySound('money', winsound.SND_ALIAS)
show()
getinput()
play()
mainloop()
They aren't running out of order. Throw in a few print statements to see the order they are executing in. The problem is that until the event loop (mainloop) runs, tkinter doesn't have an opportunity to update the display. In your code mainloop won't run until the other functions finish.
To force the display to update you can call root.update_idletasks().

Categories