I Would like to create a little Game. Now my Problem is that nothing is happening when I'm pressing the Button:
Main Code:
from tkinter import *
import os
def Aufgabe():
os.system('python Test.py')
def Wahrheit():
os.system('python Test.py')
master= Tk()
master.geometry("200x60")
master.configure(bg='light blue')
Button(master, text="Aufgabe", command= print(Aufgabe)).place(x=20, y=20)
Button(master, text="Wahrheit", command="Wahrheit").place(x=100, y=20)
mainloop()
Test.py Code:
import random
print(random.randint(1,6))
Related
I am doing a project that uses many tkinter buttons on different windows and I want to be able to close a window and run a function at the same time using lambda but the window doesn't close. If I stop using lambda it works again. I tried in repl and in idle but got the same result. This works:
tk = Tk()
tk.geometry('500x300')
def function():
print('hi')
btn = Button(tk, text='hi', command = tk.destroy)
btn.pack()
tk.mainloop()
but this doesn't:
from tkinter import*
tk = Tk()
tk.geometry('500x300')
def function():
print('hi')
btn = Button(tk, text='hi', command = lambda:[ function(),tk.destroy])
btn.pack()
tk.mainloop()
why?
Instead of using:
btn = Button(tk, text='hi', command = lambda:[ hello.hi(),tk.destroy])
use:
btn = Button(tk, text='hi', command = lambda:[hello.hi(),tk.destroy()])
and it should work.
So I am making a project that outputs responses from Google Sheets that asks me and gives me video ideas. I just need help figuring out how to make Tkinter output the whole response into a window.
Here is the main code for the project I am working on:
import pygsheets
import random
import tkinter
import numpy as np
gc = pygsheets.authorize()
sh = gc.open('Give Me Video Ideas (Responses)')
wks = sh.sheet1
for row in wks:
print(list(row))
And here is the Tkinter code I have so far:
import sys
import os
from tkinter import *
window=Tk()
window.title("Give me Video Ideas")
window.geometry('550x200')
def run():
os.system('python anothergithub.py')
btn = Button(window, text="Click Me", bg="black", fg="white",command=run)
btn.grid(column=0, row=0)
window.mainloop()
This is if you have variables in you deal line run(string)
import sys
import os
from tkinter import *
window=Tk()
window.title("Give me Video Ideas")
window.geometry('550x200')
def run():
os.system('python anothergithub.py')
btn = Button(window, text="Click Me", bg="black", fg="white",command=lambda: run("Hey"))
btn.grid(column=0, row=0)
window.mainloop()
import tkinter as tk
from tkinter import filedialog, Text
from subprocess import call
import os
root = tk.Tk()
def buttonClick():
print('Button is clicked')
def openAgenda():
call("cd '/media/emilia/Linux/Programming/PycharmProjects/SmartschoolSelenium' && python3 SeleniumMain.py",
shell=True)
return
canvas = tk.Canvas(root, height=700, width=700, bg='#263D42')
canvas.pack()
frame = tk.Frame(root, bg='white')
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
openFile = tk.Button(root, text='Open file', padx=10,
pady=5, fg="white", bg='#263D42', command=openAgenda)
openFile.pack()
root.mainloop()
the script it calls opens a new browser window, after finishing entering text in that window, it opens a new browser windows and loops.
meanwhile the tkinter button stays clicked, visually.
the reason your Tk GUI freezes is because you have everything running on 1 thread. The mainloop is haulted by the submit function call which must be taking a "long time", so you probably see "Not Responding" appear in your Tk window when you click the button. To fix this, you need spawn a separate thread for submit to run in, so that the mainloop can keep doing it's thing and keep your Tk window from freezing.
this is done using threading. Instead of your button directly calling submit, have the button call a function that starts a new thread which then starts submit. Then create another functions which checks on the status of the submit thread. You can add a status bar too
import tkinter as tk
from tkinter import filedialog, Text
from subprocess import call
import os
import threading
root = tk.Tk()
def buttonClick():
print('Button is clicked')
def openAgenda():
call("cd ' /media/emilia/Linux/Programming/PycharmProjects/SmartschoolSelenium' && python3 SeleniumMain.py",
shell=True)
canvas.update()
return
def start_Agenda_thread(event):
global Agenda_thread
Agenda_thread = threading.Thread(target=openAgenda)
Agenda_thread.daemon = True
Agenda_thread.start()
canvas = tk.Canvas(root, height=700, width=700, bg='#263D42')
canvas.pack()
frame = tk.Frame(root, bg='white')
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
openFile = tk.Button(root, text='Open file', padx=10,
pady=5, fg="white", bg='#263D42', command=lambda:start_Agenda_thread(None))
openFile.pack()
root.mainloop()
Tkinter is single-threaded: it can only do one thing at a time. While the script is running, the GUI will be frozen. You'll need to do threading, multiprocessing, or find some other way to incorporate that other script in your GUI.
Python 3.7.1 //
Tk version 8.6 //
pywinauto-0.6.8 //
PyCharm Community Edition 2020.1 x64
The objective is to have the button send keys to the window in the background.
Issue appears due to the presence of pywinauto.
If inactive:
from tkinter import *
# from pywinauto.keyboard import send_keys
def left_click(event):
# send_keys('%{TAB}')
# send_keys('{{}ENTER{}}')
print("hello")
root = Tk()
label1 = Label(root, text="Other")
label1.grid(row=0, sticky=E)
bt1_label1 = Button(root, text="Button_1", bg="red")
bt1_label1.grid(row=0, column=1)
bt1_label1.bind("<Button-1>", left_click)
root.mainloop()
Return when manually closing the tkinter window is:
hello
Process finished with exit code 0
If pywinauto is active (uncommented):
from tkinter import *
from pywinauto.keyboard import send_keys
def left_click(event):
# send_keys('%{TAB}')
# send_keys('{{}ENTER{}}')
print("hello")
root = Tk()
label1 = Label(root, text="Other")
label1.grid(row=0, sticky=E)
bt1_label1 = Button(root, text="Button_1", bg="red")
bt1_label1.grid(row=0, column=1)
bt1_label1.bind("<Button-1>", left_click)
root.mainloop()
Return when manually closing the tkinter window is:
hello
Process finished with exit code -1073740771 (0xC000041D)
Any ideas why this happens?
I am trying to run a .py file from another python code using the following sequence.
from tkinter import *
import os
import sys
def open_SS():
print('processing')
os.system('cd /home/pi/Desktop/Backup')
os.system('python p.py')
def close_window():
master.destroy()
master = Tk()
master.minsize(width=500, height=300)
Button(master, text='Exit', command=close_window).grid(row=12, column=6, sticky=W, pady=4)
Button(master, text='SS', command=open_SS).grid(row=12, column=8, sticky=W, pady=4)
mainloop( )
The Exit button does the command, but the 'SS' button does not, the word 'processing' does get printed, just the running of the p.py file. I tried running those two os.system commands on a command terminal and it works fine. p.py is supposed to input GPIO signals to a Raspberry Pi.
from tkinter import *
import os
import sys
master = Tk()
def open_SS():
print('processing')
os.system("python /home/pi/Desktop/Backup/p.py")
btn=Button(master,text="click here")
btn.grid(row=0,column=1)
btn.bind("<Button>",open_SS)