sorry please bear with me i'm struggling. i've searched and i dont fully understand where i'm going wrong even though there are already posts covering the exact subject.
The short story is im running a process and is it runs through said process i want the text to change to red from black as it runs through. Now the way i am trying to do this is from outside of the classs.
If i can draw your attention to def C():
once the program has started from the start button, variable start is set to 1. i want the text in Application.createWidgets() under message statusStartTk to change to red. I am trying to do this in C() with Application.statusStartTk.config(fg='red')
Now its probably obvious to you clever guys where i'm going wrong and would appreciate being put right. i've spent a few hours trying different things and searching to no avail. You can probably see where i've commented bits and bobs out where i've been trying things.
Also i'd just like to say i appreciate the very fast and helpful responses you guys are coming back with so far, so thank you, again, and in anticipation.
anyway, here's the code:
import time
from tkinter import *
import tkinter as Tk
import tkinter
#sets up Tkinter GuI
root = Tk.Tk()
#Set Adjustable Variables for GUI
startText=StringVar()
startText.set("Start Brew!")
def a():
#print('a')
print('start = ', start)
def b():
print('b')
def c(): #GUI ASSISTANCE STATUS WINDOW
print('c')
if start==1:
Application.statusStartTk.config(fg='red')
def main():
a()
b()
c()
class Application(Tk.Frame):
def __init__(self, master=None):
Tk.Frame.__init__(self, master)
self.createWidgets()
def createWidgets(self):
#Set start buttons and sink and disable if brew started
self.startButton = Tk.Button(root, textvariable=startText, command=Application.startBrew)
self.startButton.place(x=902,y=60)
#Plant Status List
statusTitle = "Plant Status Update:"
statusTitleTk = Message(root,width=200,font=("Tk Default Font",16), text=statusTitle)
statusTitleTk.place(x=1090,y=40)
statusStart = "- Brew Started"
statusStartTk = Message(root,width=200, text=statusStart)
statusStartTk.place(x=1100,y=70)
self.onUpdate()
def startBrew():
print('brew has started')
global start,startButton
start=1
startText.set("Brew Started")
Application()
def onUpdate(self):
root.update_idletasks()
main()
#str(variable1.get())
self.after(5, self.onUpdate)
app = Application(master=root)
root.mainloop()
Related
I decided I want to learn how to make GUIs with something more than entry fields and buttons, so I'm starting off with tabs. After a little bit of research I made myself a program, but don't know why it doesn't work.
# --IMPORTS--
from tkinter import *
import tkinter.ttk as ttk
import time
# --CLASSES--
class Gui:
def __init__(self):
self.root = Tk()
self.root.title("tab test")
def setup(self):
# tabs
tabc = ttk.Notebook(self.root)
tab1 = ttk.Frame(tabc)
tabc.add(tab1, text="test 1")
tabc.grid()
def run(self):
self.root.mainloop()
# --MAIN--
if __name__ == "__main__":
gui = Gui()
gui.run()
When I run the program I just get a blank screen (see screenshot) and there is no way to see if there is a tab, let alone which one is selected.
Like I said, I don't see the reason why it isn't working. There are no error messages to point me in the right direction and I'm not 100% sure on how tabs work to begin with, so I thought I'd ask here. I've tried changing .grid() to .pack() but I think it's more of an error on my end than a bug with tkinter. Thanks in advance!
you have to run your setup method.
# --MAIN--
if __name__ == "__main__":
gui = Gui()
gui.setup()
gui.run()
Im new to programming, and really only doing this for a school project. Im trying to make a GUI that has a series of buttons that when pressed will run a specific emulator. When I try to run this I get a error saying "z26" is undefined. Im not quite sure on how to actually define it.
from tkinter import *
import os
class Application(Frame):
def __init__(self, master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
self._button = Button(self, text = "Atari", command = self._openFile)
self._button.grid()
def _openFile(self):
os.startfile(z26.exe)
root = Tk()
root.title("Arcade")
root.geometry("200x85")
app = Application(root)
root.mainloop()
The problem is that you are using x26.exe as a literal, and it is getting evaluated as though it were part of the Python program itself.
Instead, put the path with quotequotations, to make it a string:
os.startfile('path/z26.exe')
See also the Python documentation for os.startfile(path[, operation]).
I'm making an app which has a sort of splash screen made in tkinter, and I would like it to close and call and run another part of the app, however i cannot for the life of me figure out why my bind won't work.
Do keep in mind I started python about 2 weeks ago so I'm still very much a learner, any help would be greatly appreciated!
I am aware that similar questions have been answered on here, however none of the questions have the windows as part of a class, and I'm having a hard time implementing the solutions into my code as a result.
The code:
from Tkinter import *
from PIL import ImageTk
from PIL import Image
import time
class intro(Frame):
global master
master = Tk()
#master.attributes("-fullscreen", TRUE)
global img
img = ImageTk.PhotoImage(Image.open("dorina.jpeg"))
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid()
self.nameLabel = Label(master, image=img)
self.nameLabel.grid()
checker = False
self.bind("<Return>", lambda e: self.destroy())
if __name__ == "__main__":
guiFrame = intro()
guiFrame.mainloop()
This time as an answer and I hope it helps.
The following works for me:
import Tkinter as tk
class simpleapp_tk(tk.Tk):
def __init__(self, parent):
## class derives from Tkinter --> call its constructor
tk.Tk.__init__(self, parent)
## keep track of the parent
self.parent = parent
self.bind("<Return>", lambda x: self.destroy())
if __name__ == "__main__":
app = simpleapp_tk(None)
app.title('test')
#app.wm_attributes('-topmost', 1) # always on top
app.mainloop()
I've been creating a program for myself and my company recently wants to use it. However the end-users have no python experience so I'm making a GUI for them using EasyGUI. All they have to do is click a shortcut on their desktop (I'm using pythonw.exe so no box shows up). The Process takes roughly 10 seconds to run but there is a blank screen when doing so.
My question is: Can i have a message box that says "Running..." while the function runs and then close when the entire process completes?
Bonus points: to have a progress bar while process runs.
Now I've done some searching but some of this stuff is over my head (I'm fairly new to Python). I'm not sure how to incorporate these parts into my code. Is there anything that is easy like EasyGUI to solve my problem? Thanks!
Related posts:
Python- Displaying a message box that can be closed in the code (no user intervention)
How to pop up a message while processing - python
Python to print out status bar and percentage
If you absolutely need to see my code i can try and re-create it without giving away information. The higher-ups would appreciate me not giving away information about this project - Security is tight here.
I've written a little demo for you. Don't know if it's exactly what you wanted...
The code uses threading to update the progressbar while doing other stuff.
import time
import threading
try:
import Tkinter as tkinter
import ttk
except ImportError:
import tkinter
from tkinter import ttk
class GUI(object):
def __init__(self):
self.root = tkinter.Tk()
self.progbar = ttk.Progressbar(self.root)
self.progbar.config(maximum=10, mode='determinate')
self.progbar.pack()
self.i = 0
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.work_thread = threading.Thread(target=work)
self.work_thread.start()
self.root.after(50, self.check_thread)
self.root.after(50, self.update)
def check_thread(self):
if self.work_thread.is_alive():
self.root.after(50, self.check_thread)
else:
self.root.destroy()
def update(self):
#Updates the progressbar
self.progbar["value"] = self.i
if self.work_thread.is_alive():
self.root.after(50, self.update)#method is called all 50ms
gui = GUI()
def work():
#Do your work :D
for i in range(11):
gui.i = i
time.sleep(0.1)
gui.root.mainloop()
Let me know if that helps :)
I'm using the slider to update my visualization, but the command updateValue is sent everytime I move the slider thumb, even for intermediate values.
Instead I want to trigger it only when I release the mouse button and the interaction is complete.
self.slider = tk.Scale(self.leftFrame, from_=0, to=256, orient=tk.HORIZONTAL, command=updateValue)
How can I trigger the function only once, when the interaction is ended ?
This is quite an ancient question now, but in case anyone stumbles upon this particular problem just use the bind() function and the "ButtonRelease-1" event like so:
import Tkinter as tk
class App:
def __init__(self):
self.root = tk.Tk()
self.slider = tk.Scale(self.root, from_=0, to=256,
orient="horizontal")
self.slider.bind("<ButtonRelease-1>", self.updateValue)
self.slider.pack()
self.root.mainloop()
def updateValue(self, event):
print self.slider.get()
app=App()
Hope this helps anyone!
You can't.
What you can do instead is have your command delay any real work for a short period of time using 'after'. Each time your command is called, cancel any pending work and reschedule the work. Depending on what your actual requirements are, a half second delay might be sufficient.
Another choice is to not use the built-in command feature and instead use custom bindings. This can be a lot of work to get exactly right, but if you really need fine grained control you can do it. Don't forget that one can interact with the widget using the keyboard in addition to the mouse.
Here's a short example showing how to schedule the work to be done in half a second:
import Tkinter as tk
#create window & frames
class App:
def __init__(self):
self.root = tk.Tk()
self._job = None
self.slider = tk.Scale(self.root, from_=0, to=256,
orient="horizontal",
command=self.updateValue)
self.slider.pack()
self.root.mainloop()
def updateValue(self, event):
if self._job:
self.root.after_cancel(self._job)
self._job = self.root.after(500, self._do_something)
def _do_something(self):
self._job = None
print "new value:", self.slider.get()
app=App()