I just made a simple project with Tkinter GUI, but when I launch it and enter username, its window stops responding 'til the requests and instaloader processes are done, then it will be ok. Can I make a 'please wait' thing to avoid not responding? or does it get better if I migrate to another GUI?
from tkinter import *
import instaloader
from PIL import ImageTk, Image
import requests
from io import BytesIO
def insta(username):
L = instaloader.Instaloader()
profile = instaloader.Profile.from_username(L.context, username)
label2.config(text=profile.full_name)
label3.config(text=profile.biography)
url=profile.get_profile_pic_url()
response = requests.get(url)
img_data = response.content
img = ImageTk.PhotoImage(Image.open(BytesIO(img_data)))
panel = Label(image=img)
panel.place(x=150,y=100)
label4.config(text="Done")
window = Tk()
window.geometry("600x600")
window.maxsize(600, 600)
window.minsize(600, 600)
window.title("Instagram Profile Downloader")
# label
label = Label(window, text="Enter UserName to Download Profile Image:",
fg="black", bg="#f4b265")
label.place(x=180, y=20)
label2 = Label(window, text="")
label2.place(x=100, y=70)
label3 = Label(window, text="")
label3.place(x=100, y=100)
label4 = Label(window, text="", fg="red")
label4.place(x=380, y=50)
# button
def butt():
if input.get() == "":
label4.config(text="Please Enter Username")
return
else:
insta(input.get())
button = Button(window, text="Download", fg="white",
bg="#095e95", command=butt)
button.place(x=310, y=47)
# input
input = Entry(window)
input.place(x=180, y=50)
window.mainloop()
Tkinter stuff is running in the main thread and so when you call something in the main thread that takes time the GUI will be blocked while that function is running. To solve this you need to use threads to make sure the call to insta is running separate from the main thread where tkinter is running but also need to make sure that you dont call tkinter functions on that other thread because tkinter only works in the main thread.
Here's an exemple of how you could acheive it:
import threading
class InstaThread:
def __init__(self, username):
self.profile = None
self.img_data = None
# Disable the button while instaloader is running
button["text"] = "Loading..."
button["state"] = DISABLED
self.thread = threading.Thread(target=self.insta, args=(username,))
self.thread.start()
self.check_thread()
# Check periodically if the function has alread run
def check_thread(self):
if self.thread.is_alive():
window.after(100, self.check_thread)
else:
label2.config(text=self.profile.full_name)
label3.config(text=self.profile.biography)
panel = Label(window)
img = ImageTk.PhotoImage(Image.open(BytesIO(self.img_data)))
# Also updated this so that the image would really show in the panel
panel.image = img
panel.config(image = img)
panel.place(x=150,y=100)
label4.config(text="Done")
# Reset button
button["text"] = "Download"
button["state"] = NORMAL
# Function that will be running in other thread and updating the profile and img_data varibles of this class
def insta(self, username):
L = instaloader.Instaloader()
self.profile = instaloader.Profile.from_username(L.context, username)
url=self.profile.get_profile_pic_url()
response = requests.get(url)
self.img_data = response.content
Now in the Butt function you just need to call InstaThread(input.get()) instead of insta(input.get()) and like this when you click the button it will disable and say Loading while the function is running and the rest of the GUI will continue working fine
Another suggestion I have is for you to make a class for your GUI so that you have it all in one place and can access it anywhere inside the class so that you dont have to be having global variables for buttons and labels.
Related
I am trying to create an app using the Tkinter Python library, and I created a preferences popup. I want to add checkboxes to it, and I want to do it in Frames via pack().
I want something like this:
Expected Result (IK it's Edited but Proof of Concept)
This is what I'm getting:
Actual Result (Look at Bottom of Image)
This is what I wrote:
# Import Libraries
from tkinter import *
from tkinter import ttk
from tkinter import simpledialog, messagebox
from tkinter.filedialog import asksaveasfile
from pygame import mixer as playsound
from datetime import datetime as date
from time import sleep
import pyttsx3
import json
import os
# Set Initial Window
window = Tk()
window.title("TTSApp")
window.geometry('500x580')
window.resizable(width=False,height=False)
playsound.init()
# Settings and Menu
preferences = {}
def preferencesHandler():
if os.path.exists('preferences.pref'):
preferences = {'AutoSave':True,'AutoSavePrompt':True,'AutoSaveAutomaticLoad':False}
with open('preferences.pref', 'w') as pref:
json.dump(preferences, pref)
else:
preferences = json.load(open('preferences.pref', 'r'))
pref.close()
sessionOptions = {'SessionName':'Untitled','VoiceSpeed':100}
def topmenucommands_file_newsession():
messagebox.showerror("New Session", "I haven't done this yet... you shouldn't even be able to see this...")
def topmenucommands_file_preferences():
preferencesWin = Toplevel(window)
preferencesWin.geometry("350x500")
preferencesWin.title("Preferences")
preferences_autosave = BooleanVar()
preferences_autosaveprompt = BooleanVar()
preferences_autosaveautomaticload = BooleanVar()
def topmenucommands_file_preferences_changed(*args):
with open('preferences.pref') as pref:
preferences['AutoSave'] = preferences_autosave.get()
preferences['AutoSavePrompt'] = preferences_autosaveprompt.get()
preferences['AutoSaveAutomaticLoad'] = preferences_autosaveautomaticload.get()
json.dump(preferences, pref)
pref.close()
Label(preferencesWin, text="Preferences", font=('helvetica', 24, 'bold')).pack()
autosave_container = Frame(preferencesWin,width=350).pack()
Label(autosave_container, text="Create Autosaves:", font=('helvetica', 12, 'bold')).pack(side=LEFT)
ttk.Checkbutton(autosave_container,command=topmenucommands_file_preferences_changed,variable=preferences_autosave,onvalue=True,offvalue=False).pack(side=RIGHT)
window.wait_window(preferencesWin)
pref.close()
def topmenucommands_session_renamesession():
topmenucommands_session_renamesession_value = simpledialog.askstring(title="Rename Session",prompt="New Session Name:")
sessionOptions['SessionName'] = topmenucommands_session_renamesession_value
topmenu = Menu(window)
topmenu_file = Menu(topmenu, tearoff=0)
#topmenu_file.add_command(label="New Session")
#topmenu_file.add_command(label="Save Session")
#topmenu_file.add_command(label="Save Session As...")
topmenu_file.add_command(label="Preferences", command=topmenucommands_file_preferences)
topmenu.add_cascade(label="File", menu=topmenu_file)
topmenu_session = Menu(topmenu, tearoff=0)
topmenu_session.add_command(label="Rename Session", command=topmenucommands_session_renamesession)
topmenu.add_cascade(label="Session", menu=topmenu_session)
# Create All of the Widgets and Buttons and Kiknacks and Whatnot
# Input Window
inputText = Text(window,height=20,width=62)
inputText.pack()
# Label for Speed Slider
speedText = Label(window, text='Voice Speed', fg='black', font=('helvetica', 8, 'bold'))
speedText.pack()
# Speed Slider
speed = Scale(window, from_=50, to=200, length=250, tickinterval=25, orient=HORIZONTAL, command=speedslidersavestate)
speed.set(100)
speed.pack()
# Dropdown for Voice Selection
voice = OptionMenu(window, voiceSelection, *voiceNames.keys())
voice.pack()
# Warning/Notice Label
warning = Label(window, text='', fg='red', font=('helvetica', 12, 'bold'))
warning.pack()
# Container for All Preview and Save (and PreviewRaw)
buttons = Frame(window)
buttons.pack()
# PreviewRaw Button; Huh... There's Nothing Here
# Preview Button
preview = Button(buttons,text='Preview',height=5,width=25,command=preview)
preview.pack(side=LEFT)
# Save Button
save = Button(buttons,text='Save to File',height=5,width=25,command=save)
save.pack(side=RIGHT)
window.config(menu=topmenu)
preferencesHandler()
window.mainloop()
Did I do something wrong or is there a better way to go about this or is this question a mess (this is my first time doing this)? Also, I clipped out all of the unnecessary content.
Edit: Added More Code
I figured it out. Apparently, I needed to pack() the Frame separately.
The answer was:
autosave_container = Frame(preferencesWin,width=350)
autosave_container.pack()
Instead of:
autosave_container = Frame(preferencesWin,width=350).pack()
I'm very new to python programming and I've been unable to find out how to get this to function properly. I'm using frames to show a login screen prior to loading the main inventory frame. I intend to record changes to inventory per user, so this is necessary for the program to function properly.
The program calls the Loginscreen() and LoginFrameGenerate() functions to generate the top menu and the frame including labels and entry fields. (Using Tkinter)
The "Log In" button, once pressed, calls the loginfun() function.
The loginfun() function is supposed to use an if statement to check the username and password and if they are correct, remove the login screen frame. Once the login screen frame has been removed, the mainframe and menu functions are called.
Unfortunately, the login screen frame will not go away when using the destroy() command. I can't move forward until I can get this working. Any guidance would be greatly appreciated.
I tried the function without re-initializing the frame inside the loginfun() function, but it generates a name error. I no longer get the name error, but the frame doesn't get destroyed. I've searched google and stack overflow for hours and the only thing I could find regarding Tkinter destroy() was in reference to classes. I'm beginning to think I've been coding this wrong and I should have made each frame a separate class.
from tkinter import *
import tkinter.messagebox
import sys
root = Tk()
root.iconbitmap('Favicon.ico')
testusername = "Admin"
testpassword = "Pass"
CurrentUser = StringVar()
Pass = StringVar()
root.state("zoomed")
def Exitbtnclick():
sys.exit(0)
def logout():
MainFrame=Frame(root)
MainFrame.destroy()
loginScreen
loginFrameGenerate
def loginScreen():
loginMenu = Menu(root)
root.configure(menu=loginMenu)
FileMenu=Menu(loginMenu)
loginMenu.add_cascade(label="File", menu=FileMenu)
FileMenu.add_cascade(label="Exit",command=Exitbtnclick)
def loginFrameGenerate():
#THIS CREATES THE LOGIN FRAME
logframe = Frame(root)
logframe.grid()
UL = Label(logframe, text="Username:", font="Arial 10 bold")
UL.grid(row=0, column=0, sticky="E")
UE = Entry(logframe, textvariable=CurrentUser)
UE.grid(row=0,column=1)
PL = Label(logframe, text="Password:", font="Arial 10 bold")
PL.grid(row=1, column=0, sticky="E")
PE = Entry(logframe, textvariable=Pass)
PE.grid(row=1,column=1)
loginbtn = Button(logframe, text="Log In", command=loginfun)
loginbtn.grid(row=3,columnspan=2)
def loginfun():
#THIS FUNCTION IS CALLED WHEN THE "LOG IN" BUTTON IS PRESSED
global testusername
global testpassword
logframe = Frame(root)
if (CurrentUser.get() == testusername) and (Pass.get() == testpassword):
logframe.destroy() #THIS IS THE PART THAT DOES NOT WORK
initializeMainMenu
initializeMainFrame
else:
tkinter.messagebox.showinfo("Error!", "Invalid Username/Password")
return
def initializeMainMenu():
mainMenu = Menu(root)
root.configure(menu=mainMenu)
FileMenu = Menu(mainMenu)
UserMenu = Menu(mainMenu)
ItemsMenu = Menu(mainMenu)
ReportMenu = Menu(mainMenu)
mainMenu.add_cascade(label="File", menu=FileMenu)
mainMenu.add_cascade(label="Users", menu=UserMenu)
mainMenu.add_cascade(label="Items", menu=ItemsMenu)
mainMenu.add_cascade(label="Reports", menu=ReportMenu)
FileMenu.add_separator()
FileMenu.add_command(label="Log Out/Switch User", command=logout)
FileMenu.add_command(label="Exit", command=Exitbtnclick)
UserMenu.add_command(label="Add/Remove Users",command=random)
def initializeMainFrame():
##Main Inventory Screen To Be Filled in once the user is logged in
MainFrame = Frame(root)
MainFrame.grid()
loginScreen()
loginFrameGenerate()
root.mainloop()
I am working an a tool where-in I am getting the initial user credentials and an unique identifier via Tkinter GUI interface. Post that after a lot of data fetching and processing I would get a report into an excel sheet using xlsxwriter package.
I generally exit/close the tkinter window using destroy() method on click of a button. Here, I want to show the user the status of the report creation in a Tkinter messagebox and then close the main window.
Note: I am using .pyw extension, so that the end user who is using the tool shouldn't see the console. So once the user hits the submit button, I will show a label at the footer of the window saying "Processing ..."
Sample code:
from tkinter import *
#Some other libraries are imported
mScrn = Tk()
mScrn.title("Report Generation Tool v1.0")
mScrn.geometry("200x180")
mScrn.resizable(False, False)
tk_uid_lbl = Label(mScrn, text="MVS1 Username")
tk_uid_lbl.pack()
tk_uid_lbl.place(x=20,y=20)
uid = StringVar()
tk_uid = Entry(mScrn, bd=3, textvariable=uid)
tk_uid.pack()
tk_uid.place(x=150, y=20)
tk_pwd_lbl = Label(mScrn, text="MVS1 Password")
tk_pwd_lbl.pack()
tk_pwd_lbl.place(x=20,y=60)
pwd = StringVar()
tk_pwd = Entry(mScrn, bd=3, show='*', textvariable=pwd)
tk_pwd.pack()
tk_pwd.place(x=150, y=60)
tk_ver_lbl = Label(mScrn, text="Version #")
tk_ver_lbl.pack()
tk_ver_lbl.place(x=20,y=100)
ver = StringVar()
tk_ver=Entry(mScrn, bd=3, textvariable=ver)
tk_ver.pack()
tk_ver.place(x=150, y=100)
tk_sub_button = Button(text='Submit', command = show_footer)
tk_sub_button.pack()
tk_sub_button.place(x=150, y=150)
mScrn.mainloop()
#The data provided in the GUI is used for access and a lot of process goes on
#Close the Tkinter window post the process is done
Thanks in Advance. I am using Python3
I am having a hard time understanding your question. My understanding is that using destroy() is exactly what you are looking for. Use destroy() when you are finished. You basically already answered your own question. I would find it helpful if you could explain your question more thoroughly. I agree with Goyo but I cannot comment.
I don't know how to get the data before closing the mainloop(). In
that aspect once that is closed I cannot show the label on the GUI and
then close with user consent (i.e. after clicking 'ok' in message box)
I don't undestand where is your problem, you can save your data with a lot ways, list, module, object, file, etc.
import tkinter as tk
import random
import threading
import time
# Simulate a process
def get_data(callback):
while True:
if len(data) == 10:
break
time.sleep(.5)
data.append(random.randint(1, 200))
callback()
def wait_end(label, tk_var_end, num=0):
label["text"] = "Processing " + " ." * num
num += 1
if num == 4:
num = 0
if not tk_var_end.get():
mScrn.after(500, wait_end, label, tk_var_end, num)
def execute():
for entry in (tk_uid, tk_pwd, tk_ver):
entry['state'] = tk.DISABLED
tk_sub_button.destroy()
tk_process_lbl = tk.Label(mScrn)
tk_process_lbl.pack()
tk_process_lbl.place(x=150,y=150)
tk_var_end = tk.BooleanVar(False)
wait_end(tk_process_lbl, tk_var_end)
process = threading.Thread(
target=get_data,
kwargs=(dict(callback=lambda: tk_var_end.set(True)))
)
process.start()
mScrn.wait_variable(tk_var_end)
mScrn.after(500, tk_process_lbl.config, dict(text='Process completed'))
mScrn.after(1500, mScrn.quit)
mScrn = tk.Tk()
data = []
mScrn.title("Report Generation Tool v1.0")
mScrn.geometry("400x180")
mScrn.resizable(False, False)
tk_uid_lbl = tk.Label(mScrn, text="MVS1 Username")
tk_uid_lbl.pack()
tk_uid_lbl.place(x=20,y=20)
uid = tk.StringVar()
tk_uid = tk.Entry(mScrn, bd=3, textvariable=uid)
tk_uid.pack()
tk_uid.place(x=150, y=20)
tk_pwd_lbl = tk.Label(mScrn, text="MVS1 Password")
tk_pwd_lbl.pack()
tk_pwd_lbl.place(x=20,y=60)
pwd = tk.StringVar()
tk_pwd = tk.Entry(mScrn, bd=3, show='*', textvariable=pwd)
tk_pwd.pack()
tk_pwd.place(x=150, y=60)
tk_ver_lbl = tk.Label(mScrn, text="Version #")
tk_ver_lbl.pack()
tk_ver_lbl.place(x=20,y=100)
ver = tk.StringVar()
tk_ver= tk.Entry(mScrn, bd=3, textvariable=ver)
tk_ver.pack()
tk_ver.place(x=150, y=100)
tk_sub_button = tk.Button(text='Submit', command = execute)
tk_sub_button.pack()
tk_sub_button.place(x=150, y=150)
mScrn.mainloop()
print(data)
But, you can also make your own class which will inherit of Tk, in this class you could override the quit or destroy method of Tk.
i have made a tkinter application on which i have a submit button, when i press this button a crawl request goes to request.get(url) method and it starts with the crawling and the tkinter becomes inactive untill it gives me the response. but i want another button "stop" in tkinter app which when pressed stops the process going in the background and next time i want i can press the submit button to again start the process.
i have tried destroy and quit methods available in tkinter but they closes the tkinter widget itself.
my code is :-
#!/usr/bin/python
# -*- coding: latin-1 -*-
from Tkinter import *
import thread
from tool_crawler import *
import tkMessageBox
import ttk
# function which accepts url from GUI and pass it further for the
def crawl(url):
time_delay = time_delay_box.get()
if url == "Please enter URL.......":
tkMessageBox.showerror("Error(错误)", "Please enter a valid URL(请输入有效网址)")
elif not url:
tkMessageBox.showerror("Error(错误)", "Please enter a valid URL(请输入有效网址)")
else:
tree.heading("#0", text=url)
links = find_links(url, time_delay)
#print links
for i, link in enumerate(links):
tree.insert("", i, str(i), text=link)
tree.bind("<Double-1>", OnDoubleClick)
# function will invoke when any node in tree is double clicked
def OnDoubleClick(event):
item = tree.identify('item',event.x,event.y)
url = tree.item(item,"text")
index = tree.index(item)
parent = tree.parent(item)
crawl_child(url,index,parent)
def crawl_child(url,index,parent):
links = find_links(url, time_delay)
#print links
for i, link in enumerate(links):
if not parent:
tree.insert(str(index), i, str(index)+"."+str(i), text=link)
else:
tree.insert(str(parent)+"."+str(index), i, str(parent)+"."+str(index)+"."+str(i), text=link)
# tkinter object
top = Tk()
top.configure(background='Lavender')
# window title
top.title("Fan Wan Crawler ")
# tkinter minimum and maximum size
top.minsize(width=1460, height=800)
top.maxsize(width=1460, height=800)
# url entry area i.e. text box to enter the URLS
url = Entry(top, bd =3, width = 180)
url.insert(0, "Please enter URL.......")
url.pack(side = TOP)
# function to show and hide the text on entry and exit
def default(event):
current = url.get()
if current == "Please enter URL.......":
url.delete ("0", END)
elif not current:
url.insert("0", "Please enter URL.......")
# code to call function default on focusin and focusout
url.bind("<FocusIn>", default)
url.bind("<FocusOut>", default)
# submit button which is performing action on submit
submit = Button(top, text="Submit(提交)", width=15, bg='lightblue', command=lambda: crawl(url.get()))
submit.pack(side = TOP)
# time delay label
time_label = Label(top, text="Time Dealy (时间延迟):", font= ("Helvetica", 12), bg="Lavender", fg = "green")
time_label.place(x=2, y=27)
# time delay Entry
time_delay_box = Spinbox(top, from_=0, to=100, width=3)
time_delay_box.place(x=175, y=27)
# time description
time_label = Label(top, text="(in sec.(以秒为单位))", font=("Helvetica", 12), bg="Lavender", fg = "green")
time_label.place(x=220, y=27)
# tree area
tree = ttk.Treeview(top, selectmode="browse", height= "36")
columns = tree.column("#0", minwidth=0, width=720, stretch=True)
tree.place(x=2,y=56)
top.mainloop()
basically i am calling find_links function which is present in sepearte file and have the function request.get to crawl the links, presently i am not using threads.So, is there any way to stop this process without using threads. i dont want to pause it i just want to kill this process.
You can use a thread to start the scraping / crawling in another thread and then create a pause / resume button that enables "pausing" and "unpausing" the spawned threads through the thread methods threading.Condition and threading.Lock()
Example of one way to set the threads up, didn't create the gui or anything just showed an example of how to use a pauseable thread with a button click in your GUI since how you choose to setup / incorporate the threads will differ. This assumes pausing and resuming will occur after the thread is finished processing the current request.get call. Trying to stop a thread dead middle of a task is an entirely different issue unto itself.
import threading, requests
import tkinter as tk
def switch_thread_states(widget):
#or whatever pause / resume text you wish for the widget / button
if widget['text'] == 'Pause'
widget['text'] = 'Resume'
else:
widget['text'] = 'Pause'
for thread in threading.enumerate():
if not isinstance(thread, threading._MainThread):
if thread.paused:
thread.resume()
else:
thread.pause()
class PauseableThread(threading.Thread):
def __init__(self, urls):
threading.Thread.__init__(self)
self.urls = urls
self.paused = False
self.pause_cond = threading.Condition(threading.Lock())
def run(self):
for url in self.urls:
with self.pause_cond:
while self.paused:
self.pause_cond.wait()
#make requests here or whatever you're doing
requests.get(url, headers={'User-Agent':'Mozilla/5.0 .....'})
def pause(self):
self.paused = True
self.pause_cond.acquire()
def resume(self):
self.paused = False
self.pause_cond.notify()
self.pause_cond.release()
#make root
#make a button that calls switch_thread_states on press
#spawn threads with urls lists etc
I've searched and found a few things on parent windows in python but that is not what I was looking for. I am trying make a simple program that opens a window and another window after that when the previous one is closed. I was also trying to implement some kind of loop or sleep time to destroy the window by default if the user does not. This is what I have (I'm new please don't laugh)
from tkinter import *
import time
root = Tk()
i = 0
if i < 1:
root.title("title")
logo = PhotoImage(file="burger.gif")
w1 = Label(root, image=logo).pack()
time.sleep(3)
root.destroy()
i = i + 1
if i == 1:
root.title("title")
photoTwo = PhotoImage(file="freedom.gif")
labelTwo = Label(root, image=photoTwo).pack()
time.sleep(3)
root.destroy()
i = i + 1
mainloop.()
Perhaps you're looking for something like this:
from tkinter import *
import time
def openNewWindow():
firstWindow.destroy()
secondWindow = Tk()
secondWindow.title("Second Window")
photoTwo = PhotoImage(file="freedom.gif")
labelTwo = Label(secondWindow, image=photoTwo).pack()
secondWindow.mainloop()
firstWindow = Tk()
firstWindow.title("First Window")
logo = PhotoImage(file="burger.gif")
w1 = Label(firstWindow, image=logo).pack()
closeBttn = Button(firstWindow, text="Close!", command=openNewWindow)
closeBttn.pack()
firstWindow.mainloop()
This creates a button in the first window, which the user clicks. This then calls the openNewWindow function, which destroys that window, and opens the second window. I'm not sure there's a way to do this using the window exit button.
To get create a more sustainable window creation, use this:
from tkinter import *
import time
def openThirdWindow(previouswindow):
previouswindow.destroy()
thirdWindow = Tk()
thirdWindow.title("Third Window")
photoTwo = PhotoImage(file="freedom.gif")
labelTwo = Label(thirdWindow, image=photoTwo).pack()
thirdWindow.mainloop()
def openSecondWindow(previouswindow):
previouswindow.destroy()
secondWindow = Tk()
secondWindow.title("Second Window")
photoTwo = PhotoImage(file="freedom.gif")
labelTwo = Label(secondWindow, image=photoTwo).pack()
closeBttn = Button(secondWindow, text="Close!", command= lambda: openThirdWindow(secondWindow))
closeBttn.pack()
secondWindow.mainloop()
def openFirstWindow():
firstWindow = Tk()
firstWindow.title("First Window")
logo = PhotoImage(file="burger.gif")
w1 = Label(firstWindow, image=logo).pack()
closeBttn = Button(firstWindow, text="Close!", command= lambda: openSecondWindow(firstWindow))
closeBttn.pack()
firstWindow.mainloop()
openFirstWindow()
This places the opening of each window in a seperate function, and passes the name of the window through the button presses into the next function. Another method would be setting the window names as global, but this is messy.
The function "lambda:" calls the function, in tkinter you must type this if you want to pass something through a command.
We initiate the whole process first first called "openFirstWindow()"