I want to push some configlines through a telnetsession from a tkinter gui.
Simplified; in my GUI I have a button. After pressing this button the telnet session starts (it fetches the config lines from an external fileshare).
But at this moment it is not very user friendly, there is now way to tell whether it is still busy or not. I want to fix this by a progress bar popup.
I've got my main script with all the fetching of the configuration files, adapting and sending it to a switch.
I got a "standalone" script with the popup progress bar.
Now I want to combine those two, but it won't work. I've read about multi-threading, but since I'm new to coding I need some help to understand how this works, if it is needed in my case
The mainscript:
#window stuff
window = Tk()
window.geometry('700x500')
window.title("Switchconfig generator")
window.lift()
def btntelnetclicked():
try:
telnet()
except:
messagebox.showinfo("Status", "Something went wrong, config was not pushed.")
#buttons
btntelnet= Button(window, text="Push config", command=btntelnetclicked)
btntelnet.grid(column=2, row=4, padx=10, pady=10)
This is offcourse just a little piece of code
The progressbar
import threading
try: import tkinter
except ImportError:
import Tkinter as tkinter
import ttk
else: from tkinter import ttk
class GUI_Core(object):
def __init__(self):
self.root = tkinter.Tk()
self.progbar = ttk.Progressbar(self.root)
self.progbar.config(maximum=4, mode='indeterminate')
self.progbar.pack()
self.b_start = ttk.Button(self.root, text='Start')
self.b_start['command'] = self.start_thread
self.b_start.pack()
def start_thread(self):
self.b_start['state'] = 'disable'
self.progbar.start()
self.secondary_thread = threading.Thread(target=arbitrary)
self.secondary_thread.start()
self.root.after(50, self.check_thread)
def check_thread(self):
if self.secondary_thread.is_alive():
self.root.after(50, self.check_thread)
else:
self.progbar.stop()
self.b_start['state'] = 'normal'
def arbitrary():
btntelnetclicked()
gui = GUI_Core()
gui.root.mainloop()
Related
actually what my code does is, asking for some information and then when he/she finishes,I will move her to the next page, which asks her for Email and password:
import tkinter as tk
from tkinter import ttk
class Bio():
def __init__(self):
self.window = tk.Tk()
self.frame = tk.Frame(self.window)
self.window.title('Data form')
self.frame.pack()
def main(self):
Bio.personaly(self)
Bio.information(self)
Bio.beruflich(self)
Bio.term_accept(self)
Bio.enter(self)
Bio.retrive(self)
self.window.mainloop() # Runs 'til i quit
well, The rest of the code is just operations
what i except is, When the user finishes and clicks the button (i have it in my code), the window is closed and a new window is opened
my question is how can i close the previous window and where can i start and open the new window?
You can do it like this withe the destroy statement:
import tkinter as Tk
from tkinter import *
root = Tk()
width= root.winfo_screenwidth()
height= root.winfo_screenheight()
root.geometry("%dx%d" % (width, height))
root.title("selfdestruction")
root.config(bg="black")
def exit():
root.destroy()
destroy_button = Button(root, text="exit",command=lambda: exit())
destroy_button.place(relx=0.5, rely=0.5, anchor="center")
root.mainloop()
I am having trouble with my program. If I pressed the button, the timer will be activated. After a few seconds, the app should open. But it doesn't work. How can I make it work? Sorry for my English. I'm from Indonesia.
Here is the code:
from tkinter import *
import os
import time
root = Tk()
my_menu = Menu(root)
root.config(menu=my_menu)
root.title("Tkinter window")
root.geometry("400x400")
def startapp():
t = 3
if time.sleep(t):
os.startfile("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe")
StartButton=Button(root, text='Start', command=startapp)
StartButton.pack()
root.mainloop()
Very simple answer!!!
def startapp():
t = 3.0
time.sleep(t)
os.startfile("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe")
time.sleep() is a function that adding delay! So you don't have to check it using another if condition...
hey beginner programmer here, making a to do list for a high school assignment and cant figure out this error. its also my first time using python as I usually use java and JavaScript for projects. Im trying to add a dark and light mode to my program. Am I doing this correctly? btw, i'm using the online ide repl.it
here's my code for reference
import tkinter
import tkinter.messagebox
from ttkthemes import ThemedStyle
import tkinter.ttk as ttk
import pickle
root = tkinter.Tk()
root.title("To-Do List")
def add_task():
task = entry_task.get()
if task != "":
listbox_tasks.insert(tkinter.END, task)
entry_task.delete(0, tkinter.END)
else:
tkinter.messagebox.showwarning(title="Warning", message="Please enter a task")
def delete_task():
try:
task_index = listbox_tasks.curselection()[0]
listbox_tasks.delete(task_index)
except:
tkinter.messagebox.showwarning(title="Warning", message="Please select a task first")
def load_tasks():
try:
tasks = pickle.load(open("tasks.dat", "rb"))
listbox_tasks.delete(0, tkinter.END)
for task in tasks:
listbox_tasks.insert(tkinter.END, task)
except:
tkinter.messagebox.showwarning(title="Warning", message="Cant find saved task file")
def save_tasks():
tasks = listbox_tasks.get(0, listbox_tasks.size())
pickle.dump(tasks, open("tasks.dat", "wb"))
# Dark and light modes
app = tk.Tk()
app.geometry("200x400")
app.title("Changing Themes")
# Setting Theme
style = ThemedStyle(app)
style.set_theme("scidgrey")
# Button Widgets
Def_Btn = tk.Button(app,text='Default Button')
Def_Btn.pack()
Themed_Btn = ttk.Button(app,text='Themed button')
Themed_Btn.pack()
# Scrollbar Widgets
Def_Scrollbar = tk.Scrollbar(app)
Def_Scrollbar.pack(side='right',fill='y')
Themed_Scrollbar = ttk.Scrollbar(app,orient='horizontal')
Themed_Scrollbar.pack(side='top',fill='x')
# Entry Widgets
Def_Entry = tk.Entry(app)
Def_Entry.pack()
Themed_Entry = ttk.Entry(app)
Themed_Entry.pack()
# Create GUI
frame_tasks = tkinter.Frame(root)
frame_tasks.pack()
listbox_tasks = tkinter.Listbox(frame_tasks, height=10, width=50)
listbox_tasks.pack(side=tkinter.LEFT)
scrollbar_tasks = tkinter.Scrollbar(frame_tasks)
scrollbar_tasks.pack(side=tkinter.RIGHT, fill=tkinter.Y)
listbox_tasks.config(yscrollcommand=scrollbar_tasks.set)
scrollbar_tasks.config(command=listbox_tasks.yview)
entry_task = tkinter.Entry(root, width=50)
entry_task.pack()
button_add_task = tkinter.Button(root, text="Add a task", width=48, command=add_task)
button_add_task.pack()
button_delete_task = tkinter.Button(root, text="Delete a task", width=48, command=delete_task)
button_delete_task.pack()
button_load_tasks = tkinter.Button(root, text="Load a task list", width=48, command=load_tasks)
button_load_tasks.pack()
button_save_tasks = tkinter.Button(root, text="Save your task list", width=48, command=save_tasks)
button_save_tasks.pack()
root = tkinter()
root.mainloop()
repl.it has nothing to do with the issue. You have lines referencing "tk", like app = tk.Tk(), but you never defined anything called tk. It looks like some of your code expects you to have imported Tkinter via import tkinter as tk, in which case tk would be valid. But you also have code expecting it to be called tkinter, like root = tkinter.Tk(). It seems like your code was inspired from multiple sources, some of which had Tkinter imported as tk, and some where it was imported as tkinter. All you have to do is replace all tks with tkinter. For example, this line:
Def_Btn = tk.Button(app,text='Default Button')
would become:
Def_Btn = tkinter.Button(app,text='Default Button')
The problem is probably coming from your import
I observed that you did not import Tkinter as tk but you keep on using the tk. try importing it
Since you have:
import tkinter
Python looks for tkinter not tk since you have not told python that you want to use the module with the alias 'tk'
Your solution is:
import tkinter as tk
You probably forgot that you were using tkinter instead of tk.
The problem is from your import statement, you imported tkinter as ttk but you kept on using tk
This is my first run at python and i am by no means a code writer. I have a project that I needed to write a GUI to command the arduino and after much googling and piecing together code I made this program which works perfectly when ran from IDLE. When I launch it from windows (double click it) or from linux (via command line python3 filler.py) it opens and closes like there is an error. I can launch other python programs by same methods with no problems. Where as its not an issue to launch it out of IDLE to make the hardware operate I just cant give up as I would like to get more knowledgeable with python for future projects. Any help would be greatly appreciated.
import tkinter as tk
import serial as s
import time as t
from tkinter import *
class Action:
def __init__(self):
def on():
ser.write(b'7')
def exit():
ser.close() # close serial port
quit()
self.window = Tk()
self.window.title("Moore Speciality Brewing")
self.window.geometry('640x480')
self.lbl = Label(self.window, text="Can Filler",fg='black',font=(None, 15))
self.lbl.place(relx=0.24, rely=0.10, height=50, width=350)
bo = Button(self.window, text="Fill Can", width=10 ,bg='red' ,command=on)
bo.place(relx=0.34, rely=0.30, height=40, width=200)
ext = Button(self.window, text="Exit", width=10, bg='white', command=exit)
ext.place(relx=0.34, rely=0.50, height=40, width=200)
class Prompt(tk.Tk):
def __init__(self):
global comm
comm = None
tk.Tk.__init__(self)
self.geometry('640x480')
self.label = tk.Label(self, text="Comm Port",fg='black',font=(None, 15))
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Get", command=self.on_button)
self.entry.place(relx=.5, rely=.5,anchor="center" )
self.label.place(relx=.5, rely=.44,anchor="center")
self.button.place(relx=.5, rely=.56,anchor="center")
def on_button(self):
comm = self.entry.get()
global ser
ser = s.Serial(comm, 9600, timeout=0) # check your com port
t.sleep(2)
Action()
self.destroy()
Prompt()
You need to call the mainloop.
Remove the call to Prompt() (last line) and substitute it with something like this (at the bottom of the script):
if __name__ == "__main__":
prm = Prompt()
prm.mainloop()
Here more on tkinter mainloop
you have already imported time in your code.
just use t.sleep(60) at the end of your code to make the cli wait for you to see if there is an error and let you debug.
at the end Prompt() is not correct. use something like this:
myPrompt = Prompt()
myPrompt.mainloop()
this part actualy calls the tkinter gui.
I'm trying to update my cursor while my program is busy.
This snippet works:
import tkinter as tk
def button():
root.configure(cursor="watch")
root = tk.Tk()
root.geometry("300x500")
button_1 = tk.Button(master=root,command=button,width=10)
button_1.grid()
root.mainloop()
When I click the button the cursor changes
But this snippet fails:
import tkinter as tk
def button():
root.configure(cursor="watch")
input("Force a pause")
root = tk.Tk()
root.geometry("300x500")
button_1 = tk.Button(master=root,command=button,width=10)
button_1.grid()
root.mainloop()
It only updates the cursor if I make another window active (or after entering some dummy input)
I've tried adding
root.configure(cursor="watch")
root.update()
but it still doesn't work (and anyway the tk man says it's a bad idea to put an update() in a callback)
Any suggestions would be welcome.
Thanks for your time.
Your code update the cursor but it's only done after your busy process is terminated.
So you can execute your busy process in a thread to prevent the user interface to freeze.
import tkinter as tk
import threading
def worker():
for x in range(0, 100000):
print(x)
root.config(cursor="arrow")
def button():
root.config(cursor="watch")
threading.Thread(target=worker).start()
root = tk.Tk()
root.geometry("300x500")
root.config(cursor="arrow")
button_1 = tk.Button(master=root, command=button, width=10)
button_1.grid()
root.mainloop()