I'm making app that uploads video to server, so I've got an START.py file which makes Tkinter window and handles all Tkinter widgets and another one (which is being run by START.py file), upload_video_to_server.py, which obviously, uploads video to server. I'm doing this using Threading so app doesn't freeze while it's uploading, so I want to make active ttk.ProgressBar which will track upload. I managed to get needed informations (file size and already uploaded size) from upload_video_to_server.py but problem is that ProgressBar is inside START.py. After googling and many tries, I saw that cyclic imports ask for importing inside function for various reasons, so I did that like this:
START.py
def chProgressBarValue(value):
progress().prog_bar["value"] = value
print "ProgBarValueChanged to: ", prog_bar["value"]
def chProgressBarMax(max):
progress().prog_bar["maximum"] = max
def progress():
global prog_bar
print "prog bar is being made"
prog_bar = ttk.Progressbar(
app, orient="horizontal",
length=200, mode="determinate",
value=5,
max=15
)
prog_bar.pack(side=TOP)
prog_bar.place(x=380, y=395)
upload_video_to_server.py
def printTotals(transferred, toBeTransferred):
print "Transferred: {0}\tOut of: {1}".format(transferred, toBeTransferred)
import START
START.chProgressBarMax(toBeTransferred)
START.chProgressBarValue(transferred)
But after first print of transferred data information, new Tkinter window shows up and freezes/crashes whole app (uploading works like a charm without importing START.py). Here is a pic of log + problem (second empty Tkinter windows SHOULDN'T be made!)
Related
I'm building a GUI app with tkinter that based on a button it will run another py file that is in the same folder as this GUI. When i'm running the app in my working environment, everything is working fine. When i'm creating an executable app and pressing the buttons, for a second i see a black window being open and then it's being closed and the function that the i assigned the button too is continuing to the next line (a messagebox to say when the script is done running) without running the py file. I'm running the .py files by using os.system('python filename.py') and not sure if that is what causing my problem. Would appreciate any help!
Edit: I saw that for some reason it says that my json file to use for google sheets is looking in the output folder where the auto-py-to-exe is directing it. i added the json file in the bundle but it still looking for it in the wrong place.
Here are a few functions that are assigned to a button click:
# This function will run the python script to create new and styled worksheet.
def run_new_worksheet_code():
return os.system("python script_to_create_automated_sheets_each_week.py")
# This function will let the code run without freezing the GUI and creating the loading bar
# once you pressed the "Create Worksheet" button.
def new_thread_for_wks_code():
global submit_thread
global progress_bar
submit_thread = threading.Thread(target=run_new_worksheet_code)
submit_thread.daemon = True
submit_thread.start()
team_lead_window.after(20, check_when_wks_is_ready)
style = ttk.Style()
style.configure("TProgressbar", background='#30D5C8', troughcolor='#DBDBDB',
bordercolor='#DBDBDB', lightcolor='#30D5C8', darkcolor='#30D5C8')
# Creating a progress bar to indicate that the hours are loading into the google sheets.
progress_bar = ttk.Progressbar(team_lead_window, orient = 'horizontal',
length =300, mode = 'determinate')
progress_bar.place(relx=0.08, rely=0.6, width=250, height=15)
# This function will check every 20 ms if the worksheet is still being prepared
# and when it will finish - a pop-up message will say it's done!
def check_when_wks_is_ready():
if submit_thread.is_alive():
team_lead_window.after(20, check_when_wks_is_ready)
loading_bar_for_wks_update()
else:
progress_bar.destroy()
tk.messagebox.showinfo('New Worksheet', 'New Worksheet Has Created Succsefully!')
I am creating a piece of software for my organisation which I recently started, and I am using the Tkinter module in Python to create my software. I have created a word processor and an online encyclopaedia for the software.
The idea is that the main program is where you can access all the applications just by the click of a button - similar to how a desktop functions. I have created the main function that will open the applications, but when you run the program, it opens the application first and then the main program where you access the application. Everything is working fine, and there are no error messages which show up when I run the program.
I've tried researching any solutions to my problem, but they don't give me anything that I can do to fix my problem. How I have structured my function for opening the applications is that I have created a function for each application and in the Button() function, that function for the application will be the command.
Here is the code for the software that I am developing:
from tkinter import *
import time
import os
import Wordee as wordee
window = Tk()
window.title("Brainwave One")
window.iconbitmap('Brainwave-One.ico')
def get_datetime():
timeVariable = time.strftime("%I:%M %p, %A %d %B %Y")
date_time.config(text=timeVariable)
date_time.after(200,get_datetime)
def wordee():
print(wordee)
wordee = Button(window,text="Wordee",font=("Calibri",23),command=wordee)
wordee.place(relx=0.25,rely=0.1875,anchor="center")
date_time = Label(window,font=("Calibri",15))
date_time.grid(row=0,column=0)
title = Label(window,text="Brainwave One",font=("Calibri",40))
title.place(relx=0.5,rely=0.05,anchor="center")
title = Label(window,text="From The Brainwave Incorporation",font=("Calibri",13))
title.place(relx=0.5,rely=0.083,anchor="center")
get_datetime()
window.mainloop()
I am building a relatively large application based on Tkinter. Whenever I make a code change, I have to reload the application manually, and then use the GUI to go back to the desired state.
For instance, if I had open an image in the app before changing the code, and I want to open it again, I have first to open the GUI, select the image from the drop-down list and so on... This is a waste of time. Is there a way to automatically reload the application/GUI after a code change in the last state it was?
This question has been asked a number of times
Proper way to reload a python module from the console
You can use reload(module) for this, but beware of nasty side effects. For example, existing code will be based on the original code, it will not magically get new attributes or baseclasses added.
Another great resource on this would be https://code.activestate.com/recipes/580707-reload-tkinter-application-like-a-browser/
After you've described you're problem a bit further in the comments, I was able to reconstruct the issue better on my end.
Here's my basic implementation for a solution to your issue:
from tkinter import *
import json
application_state = {"background": "white"}
background_colors = ["white", "red", "blue", "yellow", "green"]
def main():
root = Tk()
root.title("Test")
root.geometry("400x400")
reload_state(root)
for color in background_colors:
def change_background_wrapper(color=color, root=root):
change_background(color, root)
Button(root, text=color,command=change_background_wrapper).pack()
root.mainloop()
def change_background(color, window):
print("new color: " + color)
window.configure(bg=color)
application_state["background"] = color
update_config()
def reload_state(window):
config_file = open("config.json")
conf = json.load(config_file)
window.configure(bg=conf["background"])
def update_config():
with open("config.json", "w") as conf:
conf.write(json.dumps(application_state))
if __name__ == "__main__":
main()
In this instance, we're able to update the background color of the GUI and it will persist on each rerun of the script, until it gets manually changed again. You can apply this concept to pretty much any sort of state you have inside your application, I think this should get you started!
I have built a Python tkinter GUI application which is an application for running different tasks. The application window is divided into 2 halves horizontally, first half shows the options the user can choose for the selected menu option and second half shows the progress of the task by showing the log messages. Each task has a separate menu option, the user selects the menu option and first half is refreshed with user option along with a Submit button.
The GUI is built using the object oriented method where each task in the menu option is an class method of the GUI object.
I now have about 5-6 menu options and working fine but the code size is becoming huge and it is becoming hard to debug any issue or add new features.
Is there any way to write the method of a class in separate file which can be called from within the main class. The logging of messages in the GUI is written in the main class so if the method is written in a separate file the how will the log messages written in the other file appear in the main window.
Please suggest alternatives.
This might not help you completely, but this is what I use. I divide my tkinter code into 2 files. First gui.py contains the GUI components (widgets) and the second methods.py contains the methods.
Both the files should be in same directory.
Here is an example of a simple app that changes the label on a button click. The method change() is stored in a different file.
gui.py
from tkinter import *
from tkinter import ttk
from methods import change #Using absolute import instead of wildcard imports
class ClassNameGoesHere:
def __init__(self,app):
self.testbtn = ttk.Button(app,text="Test",command = lambda: change(self))
#calling the change method.
self.testbtn.grid(row=0,column=0,padx=10,pady=10)
self.testlabel = ttk.Label(app,text="Before Button Click")
self.testlabel.grid(row=1,column=0,padx=10,pady=10)
def main():
root = Tk()
root.title("Title Goes Here")
obj = ClassNameGoesHere(root)
root.mainloop()
if __name__ == "__main__":
main()
methods.py
from tkinter import *
from tkinter import ttk
def change(self):
self.testlabel.config(text="After Button Click")
I have my principal script running with terminal that works perfectly. Im trying to make a gui for it but im stuck at this point.
Like you see on the screen, at the start of the script it asks if it should check the database. And just after, it asks first the platform before opening the captcha for the database check. The problem happens exactly here on my GUI version, look.
Like you see, the gui starts, but when i click on check for new database, it directly opens the captcha without asking the platform... And it asks me the platform only after i solved the captcha which i dont want to after...
Here is the main testkinter.py code:
import tkinter as tk
from tkinter import messagebox
import commands
import CheckDatabase
import SetPlatformfile
def check_and_hide():
CheckDatabase.db_download(root)
checkdb.pack_forget()
checkdb1.pack_forget()
root = tk.Tk()
checkdb = tk.Button(root, text="Check for new databases", command=check_and_hide)
checkdb.pack()
checkdb1 = tk.Button(root, text="No")
checkdb1.pack()
root.mainloop()
Here is the set_platform function called in the Checkdatabse file:
import tkinter as tk
import config
from tkinter import messagebox
def set_platform(root):
platform = tk.Label(root,text="'a'|Android -- 'i'|iOS: ")
platform.pack()
androidbutton=tk.Button(root,text="Android",command=renameplatformandroid)
iosbutton=tk.Button(root,text="iOS",command=renameplatformios)
androidbutton.pack()
iosbutton.pack()
def renameplatformandroid():
config.platform = 'android'
print(config.platform)
def renameplatformios():
config.platform = 'ios'
print(config.platform)
And cuz of my checkdatabase file is really really long, i'll just put a screen at the exact moment where set_platform is called (its called in the func signup which itself is directly called at the beginning of db_download) .
I hope my question is clear! Let me know if you need more details.