0
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.

Emilia
  • 15
  • 5
  • 1
    Read [Tkinter understanding mainloop](https://stackoverflow.com/a/29158947/7414759) and [use threads to preventing main event loop from “freezing”](https://stackoverflow.com/a/16747734/7414759) – stovfl May 21 '20 at 12:06

2 Answers2

2

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
2

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