Tkinter isn't respoding while loop is running! PYTHON - 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 )

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 window not responding when push buttton

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

Key listener script with GUI won't work (Tkinter)

I need to "listen" if the user types the ENTER key while a GUI application runs in background (using Tkinter), if the user type the ENTER key a messagebox should show up, but as soon as I run the script the GUI do not appear, and if I press ENTER a huge list of errors appears in the console, and the GUI shows up (the messagebox won't). I think I'm missing something in the "listening" function:
from pynput.keyboard import Key, Listener
import logging
from tkinter import *
from tkinter import messagebox
window = Tk()
window.title("i'm listening...")
window.geometry("400x400")
def on_press(key):
logging.info(str(key))
if str(key) == "Key.enter":
msg = messagebox.showinfo( "simple message", "you clicked the enter button!")
'''
do
other
stuffs
'''
with Listener(on_press=on_press) as listener:
listener.join()
window.mainloop()
Console:
RuntimeError: main thread is not in main loop
Thanks for your help/tips!

How to open a tkinter window when a key is pressed

I have this code:
from tkinter import *
import keyboard
#console
if keyboard.is_pressed('c'):
console=Tk()
console.geometry("500x425")
console.title("Devoloper Console")
console.resizable(True,True)
console.configure(bg='gray95')
Basically, I'm trying to use the keyboard module to detect when I press C on my keyboard, and then open a Tkinter window when I press C.
The code above doesn't work (obviously) and I don't know why. However, I do know that the problem is that it isn't detecting the keypress and not a problem with the window.
You need a continuing loop to check when c is pressed.
from tkinter import *
import keyboard
x = True
while x:
if keyboard.is_pressed('c'):
x = False
console=Tk()
console.geometry("500x425")
console.title("Devoloper Console")
console.resizable(True,True)
console.configure(bg='gray95')
console.mainloop()
import tkinter as tk
import keyboard
while (not keyboard.is_pressed("c")):
pass
root = tk.Tk()
root.bind("<c>", lambda e: tk.Toplevel())
root.mainloop()
Does this do what you wanted?
Module keyboard has a function called wait. It waits for a key to be pressed. You have to use that. Also use mainloop for the tkinter window to make it run corectly.
import tkinter as tk
import keyboard
keyboard.wait("c") #< It'll wait for c to be pressed
console = tk.Tk()
console.geometry("500x425")
console.title("Devoloper Console")
console.resizable(True,True)
console.configure(bg='gray95')
...

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