How to make the (QPushButton.setDisable) to False based on the QCheckBox state? - pyqt

I wanted to simulate an installer situation where the user checks the I agree checkbox and then the next button will be active to push.
I thought that if I created a def that outputs a boolean data then I can use that data to change the QChechBox.setDisable(bool data), but I think I chose the wrong method to do such things.
This is my code:
layout = QHBoxLayout()
self.q = QCheckBox("I AGREE" , self)
self.q.setFont(QFont("Sofia Pro" , 22 , 600))
self.b = QPushButton("Next")
self.boolResult = self.boolFunc(self.q)
self.b.setDisabled(self.boolResult)
layout.addWidget(self.q)
layout.addWidget(self.b)
self.setLayout(layout)
def boolFunc(self , input):
temp = input.isChecked()
if temp == False:
return True
else:
return False
isChecked() function is not live, the function just checks the QCheckBox once.

This fixed my problem partially:
class mainWindow(QWidget):
def __init__(self) -> None:
super().__init__()
self.setWindowTitle("Main window")
# self.setStyleSheet("background-color: black")
qdarktheme.setup_theme()
self.setMinimumWidth(400)
#___________________________________
layout = QHBoxLayout()
self.q = QCheckBox("I AGREE" , self)
self.q.setFont(QFont("Sofia Pro" , 22 , 600))
self.b = QPushButton("Next")
self.q.stateChanged.connect(self.b.setDisabled)
layout.addWidget(self.q)
layout.addWidget(self.b)
self.setLayout(layout)
But the problem is when my checkbox is active, it disables the button:
How can I reverse the process?

Related

How can I delete this QGraphicsLineItem when context menu is open in a QGraphicsPixmapItem?

I'm developing a GUI where you can connect nodes between them except in a few special cases. This implementation works perfectly fine most of the time, but after some testing i found that, when I connect one QGraphicsPixmapItem with another through a QGraphicsLineItem, and the user opens the contextual menu before completing the link, the line get stuck, and it cannot be deleted.
The process to link two elements is to first press the element, then keep pressing while moving the line and releasing when the pointer is over the other element. This is achieved using mousePressEvent, mouseMoveEvent and mouseReleaseEvent, respectively.
This code is an example:
#!/usr/bin/env python3
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
class Ellipse(QGraphicsEllipseItem):
def __init__(self, x, y):
super(Ellipse, self).__init__(x, y, 30, 30)
self.setBrush(QBrush(Qt.darkBlue))
self.setFlag(QGraphicsItem.ItemIsMovable)
self.setZValue(100)
def contextMenuEvent(self, event):
menu = QMenu()
first_action = QAction("First action")
second_action = QAction("Second action")
menu.addAction(first_action)
menu.addAction(second_action)
action = menu.exec(event.screenPos())
class Link(QGraphicsLineItem):
def __init__(self, x, y):
super(Link, self).__init__(x, y, x, y)
self.pen_ = QPen()
self.pen_.setWidth(2)
self.pen_.setColor(Qt.red)
self.setPen(self.pen_)
def updateEndPoint(self, x2, y2):
line = self.line()
self.setLine(line.x1(), line.y1(), x2, y2)
class Scene(QGraphicsScene):
def __init__(self):
super(Scene, self).__init__()
self.link = None
self.link_original_node = None
self.addItem(Ellipse(200, 400))
self.addItem(Ellipse(400, 400))
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
item = self.itemAt(event.scenePos(), QTransform())
if item is not None:
self.link_original_node = item
offset = item.boundingRect().center()
self.link = Link(item.scenePos().x() + offset.x(), item.scenePos().y() + offset.y())
self.addItem(self.link)
def mouseMoveEvent(self, event):
super().mouseMoveEvent(event)
if self.link is not None:
self.link.updateEndPoint(event.scenePos().x(), event.scenePos().y())
def mouseReleaseEvent(self, event):
super().mouseReleaseEvent(event)
if self.link is not None:
item = self.itemAt(event.scenePos(), QTransform())
if isinstance(item, (Ellipse, Link)):
self.removeItem(self.link)
self.link_original_node = None
self.link = None
class MainWindow(QMainWindow):
def __init__(self):
super(QMainWindow, self).__init__()
self.scene = Scene()
self.canvas = QGraphicsView()
self.canvas.setScene(self.scene)
self.setCentralWidget(self.canvas)
self.setGeometry(500, 200, 1000, 600)
self.setContextMenuPolicy(Qt.NoContextMenu)
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec())
How can I get rid off the line before/after the context menu event? I tried to stop them, but I do not know how.
Assuming that the menu is only triggered from a mouse button press, the solution is to remove any existing link item in the mouseButtonPress too.
def mousePressEvent(self, event):
if self.link is not None:
self.removeItem(self.link)
self.link_original_node = None
self.link = None
# ...
Note that itemAt for very small items is not always reliable, as the item's shape() might be slightly off the mapped mouse position. Since the link would be removed in any case, just do the same in the mouseReleaseEvent():
def mouseReleaseEvent(self, event):
super().mouseReleaseEvent(event)
if self.link is not None:
item = self.itemAt(event.scenePos(), QTransform())
if isinstance(item, Ellipse):
# do what you need with the linked ellipses
# note the indentation level
self.removeItem(self.link)
self.link_original_node = None
self.link = None

Tkinter buttons not changing back to the correct color after state changing to active

I am making this PDF tool, and I want the buttons to be disabled until a file or files are successfully imported. This is what the app looks like at the launch:
Right after running the callback for the import files button, the active state looks like this:
I want the colors of the buttons to turn maroon instead of the original grey. They only turn back to maroon once you hover the mouse over them. Any thoughts for how to fix this? Here is the callback for the import button:
def import_callback():
no_files_selected = False
global files
files = []
try:
ocr_button['state'] = DISABLED
merge_button['state'] = DISABLED
status_label.pack_forget()
frame.pack_forget()
files = filedialog.askopenfilenames()
for f in files:
name, extension = os.path.splitext(f)
if extension != '.pdf':
raise
if not files:
no_files_selected = True
raise
if frame.winfo_children():
for label in frame.winfo_children():
label.destroy()
make_import_file_labels(files)
frame.pack()
ocr_button['state'] = ACTIVE
merge_button['state'] = ACTIVE
except:
if no_files_selected:
status_label.config(text='No files selected.', fg='blue')
else:
status_label.config(text='Error: One or more files is not a PDF.', fg='red')
status_label.pack(expand='yes')
import_button = Button(root, text='Import Files', width=scaled(20), bg='#5D1725', bd=0, fg='white', relief='groove',
command=import_callback)
import_button.pack(pady=scaled(50))
I know this was asked quite a while ago, so probably already solved for the user. But since I had the exact same problem and do not see the "simplest" answer here, I thought I would post:
Just change the state from "active" to "normal"
ocr_button['state'] = NORMAL
merge_button['state'] = NORMAL
I hope this helps future users!
As I understand you right you want something like:
...
ocr_button['state'] = DISABLED
ocr_button['background'] = '#*disabled background*'
ocr_button.bind('<Enter>', lambda e:ocr_button.configure(background='#...'))
ocr_button.bind('<Leave>', lambda e:ocr_button.configure(background='#...'))
merge_button['state'] = DISABLED
merge_button['background'] = '#*disabled background*'
merge_button.bind('<Enter>', lambda e:ocr_button.configure(background='#...'))
merge_button.bind('<Leave>', lambda e:ocr_button.configure(background='#...'))
...
...
ocr_button['state'] = ACTIVE
ocr_button['background'] = '#*active background*'
ocr_button.unbind('<Enter>')
ocr_button.unbind('<Leave>')
merge_button['state'] = ACTIVE
merge_button['background'] = '#*active background*'
merge_button.unbind('<Enter>')
merge_button.unbind('<Leave>')
...
If there are any errors, since I wrote it out of my mind or something isnt clear, let me know.
Update
the following code reproduces the behavior as you stated. The reason why this happens is how tkinter designed the standart behavior. You will have a better understanding of it if you consider style of ttk widgets. So I would recommand to dont use the automatically design by state rather write a few lines of code to configure your buttons how you like, add and delete the commands and change the background how you like. If you dont want to write this few lines you would be forced to use ttk.Button and map a behavior you do like
import tkinter as tk
root = tk.Tk()
def func_b1():
print('func of b1 is running')
def disable_b1():
b1.configure(bg='grey', command='')
def activate_b1():
b1.configure(bg='red', command=func_b1)
b1 = tk.Button(root,text='B1', bg='red',command=func_b1)
b2 = tk.Button(root,text='disable', command=disable_b1)
b3 = tk.Button(root,text='activate',command=activate_b1)
b1.pack()
b2.pack()
b3.pack()
root.mainloop()
I've wrote this simple app that I think could help all to reproduce the problem.
Notice that the state of the button when you click is Active.
#!/usr/bin/python3
import sys
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
class Main(ttk.Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__()
self.parent = parent
self.init_ui()
def cols_configure(self, w):
w.columnconfigure(0, weight=0, minsize=100)
w.columnconfigure(1, weight=0)
w.rowconfigure(0, weight=0, minsize=50)
w.rowconfigure(1, weight=0,)
def get_init_ui(self, container):
w = ttk.Frame(container, padding=5)
self.cols_configure(w)
w.grid(row=0, column=0, sticky=tk.N+tk.W+tk.S+tk.E)
return w
def init_ui(self):
w = self.get_init_ui(self.parent)
r = 0
c = 0
b = ttk.LabelFrame(self.parent, text="", relief=tk.GROOVE, padding=5)
self.btn_import = tk.Button(b,
text="Import Files",
underline=1,
command = self.on_import,
bg='#5D1725',
bd=0,
fg='white')
self.btn_import.grid(row=r, column=c, sticky=tk.N+tk.W+tk.E,padx=5, pady=5)
self.parent.bind("<Alt-i>", self.switch)
r +=1
self.btn_ocr = tk.Button(b,
text="OCR FIles",
underline=0,
command = self.on_ocr,
bg='#5D1725',
bd=0,
fg='white')
self.btn_ocr["state"] = tk.DISABLED
self.btn_ocr.grid(row=r, column=c, sticky=tk.N+tk.W+tk.E,padx=5, pady=5)
r +=1
self.btn_merge = tk.Button(b,
text="Merge Files",
underline=0,
command = self.on_merge,
bg='#5D1725',
bd=0,
fg='white')
self.btn_merge["state"] = tk.DISABLED
self.btn_merge.grid(row=r, column=c, sticky=tk.N+tk.W+tk.E,padx=5, pady=5)
r +=1
self.btn_reset = tk.Button(b,
text="Reset",
underline=0,
command = self.switch,
bg='#5D1725',
bd=0,
fg='white')
self.btn_reset.grid(row=r, column=c, sticky=tk.N+tk.W+tk.E,padx=5, pady=5)
b.grid(row=0, column=1, sticky=tk.N+tk.W+tk.S+tk.E)
def on_import(self, evt=None):
self.switch()
#simulate some import
self.after(5000, self.switch())
def switch(self,):
state = self.btn_import["state"]
if state == tk.ACTIVE:
self.btn_import["state"] = tk.DISABLED
self.btn_ocr["state"] = tk.NORMAL
self.btn_merge["state"] = tk.NORMAL
else:
self.btn_import["state"] = tk.NORMAL
self.btn_ocr["state"] = tk.DISABLED
self.btn_merge["state"] = tk.DISABLED
def on_ocr(self, evt=None):
state = self.btn_ocr["state"]
print ("ocr button state is {0}".format(state))
def on_merge(self, evt=None):
state = self.btn_merge["state"]
print ("merge button state is {0}".format(state))
def on_close(self, evt=None):
self.parent.on_exit()
class App(tk.Tk):
"""Main Application start here"""
def __init__(self, *args, **kwargs):
super().__init__()
self.protocol("WM_DELETE_WINDOW", self.on_exit)
self.set_style()
self.set_title(kwargs['title'])
Main(self, *args, **kwargs)
def set_style(self):
self.style = ttk.Style()
#('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
self.style.theme_use("clam")
def set_title(self, title):
s = "{0}".format('Simple App')
self.title(s)
def on_exit(self):
"""Close all"""
if messagebox.askokcancel(self.title(), "Do you want to quit?", parent=self):
self.destroy()
def main():
args = []
for i in sys.argv:
args.append(i)
kwargs = {"style":"clam", "title":"Simple App",}
app = App(*args, **kwargs)
app.mainloop()
if __name__ == '__main__':
main()

How to make tkinter toggle button class by deriving from tkinter.Button?

I am trying to make a toggle button class by deriving from the tkinter.Button object. To that end, I am using this StackOverflow answer and these code examples.
The problem is that I get my desired toggle behavior from the button only after I click it twice; the first two clicks, it does not enact the self.config(relief="sunken"). I tried using the command keyword argument sample from this answer and that works from the start.
import tkinter as tk
class ToggleButton(tk.Button):
def __init__(self, parent=None, toggle_text="Toggled", toggle_bg_color="green", **kwargs):
tk.Button.__init__(self, parent, **kwargs)
self.toggled = False
self.default_bg_color = self['bg']
self.default_text = self["text"]
self.toggle_bg_color = toggle_bg_color
self.toggle_text = toggle_text
self.bind("<Button-1>", self.toggle, add="+")
def toggle(self, *args):
if self["relief"] == "sunken":
self["bg"] = self.default_bg_color
self["text"] = self.default_text
self.config(relief="raised")
# self["relief"] = "raised"
self.toggled = False
else:
self["bg"] = self.toggle_bg_color
self["text"] = self.toggle_text
# self["relief"] = "sunken"
self.config(relief="sunken")
self.toggled = True
def button_placeholder():
print("TO BE IMPLEMENTED")
root = tk.Tk()
button = ToggleButton(parent=root,
toggle_text="ON", toggle_bg_color="green",
text="OFF", command=button_placeholder)
button.pack()
root.mainloop()
Here are screenshots of the behavior of the buttons after numerous clicks
After the first two clicks on the button, the expected behavior occurs. However, if the user focuses on another window (for instance by minimizing the tkinter window) and then back, again the first two clicks do not cause the desired behavior.
Can some explain this? If not, can someone provide a solution where I can have consistent behavior on toggling my button?
Information about my system
Windows 10; 64 bit
Python 3.7.3 (64 bit)
Tkinter 8.6
The problem you seem to have is that the bg parameter is not defined when you first create the button; it only gets a value assigned upon the first button press.
Then, the logic to toggle is hard to follow: you have a self.toggled boolean, yet you are testing if the button is sunken or not to differentiate between states...
I reorganized the logic to make it easier to follow; after all, toggle is a binary change from one state to another. I therefore placed the definition of the ON and OFF states in the body of the class (into two class dictionaries), and the code swaps the two configs upon toggling.
On Windows:
import tkinter as tk
class ToggleButton(tk.Button):
ON_config = {'bg': 'green',
'text': 'button is ON',
'relief': 'sunken',
}
OFF_config = {'bg': 'white',
'text': 'button is OFF',
'relief': 'raised',
}
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.toggled = False
self.config = self.OFF_config
self.config_button()
self.bind("<Button-1>", self.toggle)
def toggle(self, *args):
if self.toggled: # True = ON --> toggle to OFF
self.config = self.OFF_config
else:
self.config = self.ON_config
self.toggled = not self.toggled
return self.config_button()
def config_button(self):
self['bg'] = self.config['bg']
self['text'] = self.config['text']
self['relief'] = self.config['relief']
return "break"
def __str__(self):
return f"{self['text']}, {self['bg']}, {self['relief']}"
def button_placeholder():
print('toggling now!')
if __name__ == '__main__':
root = tk.Tk()
button = ToggleButton(root)
button.pack()
root.mainloop()
On OSX:
Where the buttons aspect is fixed, using a tk.Label can mimic the desired behavior:
import tkinter as tk
class ToggleButtonLBL(tk.Label):
ON_config = {'bg': 'green',
'text': 'button is ON',
'relief': 'sunken',
}
OFF_config = {'bg': 'white',
'text': 'button is OFF',
'relief': 'raised',
}
def __init__(self, parent, *args, command=None, **kwargs):
super().__init__(parent, *args, **kwargs)
self.toggled = False
self.config = self.OFF_config
self.config_button()
self.bind("<Button-1>", self._toggle_helper)
self.bind("<ButtonRelease-1>", self._toggle)
self.command = command
def _toggle_helper(self, *args):
return 'break'
def _toggle(self, dummy_event):
self.toggle()
self.cmd()
def toggle(self, *args):
if self.toggled: # True = ON --> toggle to OFF
self.config = self.OFF_config
else:
self.config = self.ON_config
self.toggled = not self.toggled
self.config_button()
return 'break'
def config_button(self):
self['bg'] = self.config['bg']
self['text'] = self.config['text']
self['relief'] = self.config['relief']
return "break"
def __str__(self):
return f"{self['text']}, {self['bg']}, {self['relief']}"
def cmd(self):
self.command()
def button_placeholder():
print('toggling now!')
if __name__ == '__main__':
root = tk.Tk()
button = ToggleButtonLBL(root, command=button_placeholder)
button.pack()
root.mainloop()

how entrer value in new window and passing values of wx.newindow to wx.Frame wxpython

i have a panel with button Dynamic and when i click in button i have a new window opened the problem that i need zone to enter values to edit parameter of dynamic image like that :
that my code :
import wx
class MainFrame(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self,parent,title="Myfirst",size=(800,580))
self.top = wx.Panel(self, style = wx.SUNKEN_BORDER)
self.bottom = wx.Panel(self ,style = wx.SUNKEN_BORDER)
self.left = wx.Panel(self ,style = wx.SUNKEN_BORDER, size = (250,-1))
st1 = wx.StaticText(self.bottom, -1, "show info ")
self.bottom.SetBackgroundColour('white')
dynamic=wx.Button(self.left,-1,"Dynamique",size=(110,30),pos=(50,100))
self.Bind(wx.EVT_BUTTON, self.newwindow, dynamic)
sizer1 = wx.BoxSizer(wx.VERTICAL)
sizer1.Add(self.top,1,wx.EXPAND,5)
sizer1.Add(self.bottom,1,wx.EXPAND,5)
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
sizer2.Add(self.left,0,wx.EXPAND,5)
sizer2.Add(sizer1,1,wx.EXPAND,5)
self.SetSizer(sizer2)
def newwindow(self, event):
secondWindow = window2(parent=self.left)
secondWindow.Show()
class window2(wx.Frame):
title = "new Window"
def __init__(self,parent):
wx.Frame.__init__(self,parent, -1,'Dynamic of image', size=(300,100))
panel=wx.Panel(self, -1)
self.SetBackgroundColour(wx.Colour(100,100,100))
self.Centre()
self.Show()
app = wx.App()
frame=MainFrame(None).Show()
app.MainLoop()
how can i add the zone to edit parametre like picture ?
i not sure if the newwindow what i need or dialog !!
thanks for help
I guess you will be fine with a normal new window. You can get the zone to write the parameters with a wx.TextCtrl widgets. You will need a way to export the values typed into the wx.TextCtrl so I added the style wx.TE_PROCESS_ENTER. With this style when you finish typing and press Enter you can process the typed values.
Also, there is no need to use Show() two times (secondWindow.Show() and self.Show()). One of them is enough.
Code with comments:
import wx
class MainFrame(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self,parent,title="Myfirst",size=(800,580))
self.top = wx.Panel(self, style = wx.SUNKEN_BORDER)
self.bottom = wx.Panel(self ,style = wx.SUNKEN_BORDER)
self.left = wx.Panel(self ,style = wx.SUNKEN_BORDER, size = (250,-1))
st1 = wx.StaticText(self.bottom, -1, "show info ")
self.bottom.SetBackgroundColour('white')
dynamic=wx.Button(self.left,-1,"Dynamique",size=(110,30),pos=(50,100))
self.Bind(wx.EVT_BUTTON, self.newwindow, dynamic)
sizer1 = wx.BoxSizer(wx.VERTICAL)
sizer1.Add(self.top,1,wx.EXPAND,5)
sizer1.Add(self.bottom,1,wx.EXPAND,5)
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
sizer2.Add(self.left,0,wx.EXPAND,5)
sizer2.Add(sizer1,1,wx.EXPAND,5)
self.SetSizer(sizer2)
def newwindow(self, event):
secondWindow = window2(parent=self.left)
secondWindow.Show()
class window2(wx.Frame):
title = "new Window"
def __init__(self,parent):
"""
This is similar to the class MainFrame. You define a parent wx.Panel
and all other widgets are his childs.
"""
wx.Frame.__init__(self,parent, -1,'Dynamic of image', size=(300,100))
self.panel=wx.Panel(self, -1, style=wx.SUNKEN_BORDER)
self.st = wx.StaticText(self.panel, label='modifier bornes de la dynamique', style=wx.ALIGN_CENTER)
#### Notice the wx.TE_PROCESS_ENTER style to trigger processing the input when
#### Enter is pressed. Another alternative is to put a button somewhere.
self.text = wx.TextCtrl(self.panel, size=(200, 20), style=wx.SUNKEN_BORDER|wx.TE_PROCESS_ENTER)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.st, 0, wx.EXPAND|wx.ALL, 5)
self.sizer.Add(self.text, 0, wx.ALIGN_CENTER|wx.ALL, 5)
self.panel.SetSizer(self.sizer)
self.sizer.Fit(self.panel)
#self.SetBackgroundColour(wx.Colour(100,100,100))
self.Centre()
#### No need to use Show() here since you already use it in MainFrame.newwindow()
self.Show()
#### To execute self.onEnter when Enter is pressed inside self.text
self.Bind(wx.EVT_TEXT_ENTER, self.onEnter)
def onEnter(self, event):
#### Change it to fit your needs
print(self.text.GetValue())
self.Destroy()
app = wx.App()
frame=MainFrame(None).Show()
app.MainLoop()

Tkinter: Updating Label with New Background Color (Python 3.5.3)

I am designing a simple timer using Tkinter that changes color after a certain amount of time has elasped. I have a base timer program which works well, but now I want to modify it so the background changes color.
I have if statements that trigger on the appropriate intervals and then change the class attribute assigned to the background color, but I can't get the label color to update.
I understand the "makeWidgets" function runs only once and believe this is likely the source of my problem. I've experimented breaking out this function into the main program with mixed success. I am able to get the timer to work, but still cannot get the color to change. I have also tried writing a color change function/s but haven't had any success. I am inexperienced with python, tkinter and full-disclosure, I did not design the bulk of the base timer program.
I would really appreciate any direction/advice on how to get this working. I feel that I am either close, or in need of a complete re-work. Hopefully, the former is the case.
from tkinter import *
import time
class StopWatch(Frame):
global mincount
""" Implements a stop watch frame widget. """
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self.start = 0.0
self.elapsedtime = 0.0
self.running = 0
self.timestr = StringVar()
self.makeWidgets()
self.color = 'green'
def makeWidgets(self): #this function only run once at setup
""" Make the time label. """
self.color='green' #this works
l = Label(self, textvariable=self.timestr, bg=self.color, font=("Helvetica",300), width=12, height=2)
self.setTime(self.elapsedtime)
l.pack(fill=X, expand=YES, pady=2, padx=2)
def update(self):
""" Update the label with elapsed time. """
self.elapsedtime = time.time() - self.start
self.setTime(self.elapsedtime)
self.timer = self.after(50, self.update)
def setTime(self, elap,):
global mincount
""" Set the time string to Minutes:Seconds:Hundreths """
minutes = int(elap/60)
seconds = int(elap - minutes*60.0)
hseconds = int((elap - minutes*60.0 - seconds)*100)
self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
mincount = int(elap)
if mincount>=3:
print("yellow")
self.color='yellow' #has no effect
l.config(bg='yellow') #not in scope
#CHANGE COLOR TO YELLOW - call fx?
if mincount>=5:
print("red")
#CHANGE COLOR TO RED
def Start(self):
""" Start the stopwatch, ignore if running. """
if not self.running:
self.start = time.time() - self.elapsedtime
self.update()
self.running = 1
def Stop(self):
""" Stop the stopwatch, ignore if stopped. """
if self.running:
self.after_cancel(self.timer)
self.elapsedtime = time.time() - self.start
self.setTime(self.elapsedtime)
self.running = 0
def Reset(self):
""" Reset the stopwatch. """
self.start = time.time()
self.elapsedtime = 0.0
self.setTime(self.elapsedtime)
self.color='green'
def main():
root = Tk()
sw = StopWatch(root)
sw.pack(side=TOP)
Button(root, text='Start', command=sw.Start).pack(side=BOTTOM, fill=BOTH)
Button(root, text='Stop', command=sw.Stop).pack(side=BOTTOM, fill=BOTH)
Button(root, text='Reset', command=sw.Reset).pack(side=BOTTOM, fill=BOTH)
Button(root, text='Quit', command=root.quit).pack(side=BOTTOM, fill=BOTH)
current=sw.timestr
root.mainloop()
if __name__ == '__main__':
main()
You can't use l in your setTime function : l is a local variable in makeWidgets
it can't be used in setTime. To fix it you have to make l a variable part of the class in makeWidgets : self.label = ... for exemple. And after that use your new variable self.label in setTime : self.label.config("bg"="yellow")
Something can be improved in your if statement if mincount>=3: because if it's true you change the bg and after that you check if mincount>=5. You should do this:
if mincount>=5:
...
elif mincount >=3:
...

Resources