How to make Undo and Redo for Slider Scale Value? - python

I have code below :
var = DoubleVar()
scale = Scale(root, variable = var, command=self.scalfunc, from_=4, to=40, width=40,tickinterval=0,orient=VERTICAL,length=300,highlightthickness=0, background='#333333', fg='grey', troughcolor='#333333', activebackground='#1065BF')
scale.pack(anchor=CENTER)
scale.place(x=SCwidth/1.2, y=SCheight/15)
and I have button UNDO and anther button REDO
I want when I click in that button I make the Slider Value undo or redo

This code enables the use of Control keys to change the master colors of a tkinter.tk window.
Changes to Scale are stored in list called memory using index int called counter.
Control-z will undo
Control-Z will redo
Control-x will clear the memory
It shouldn't be too difficult to modify it for your needs.
import tkinter as tk
class UndoRedo:
def __init__(self):
self.master = tk.Tk()
self.master.withdraw()
self.master.columnconfigure(0, weight = 1)
self.master.columnconfigure(1, weight = 1)
self.color = 15790320 # #f0f0f0 = SystemButtonFace
self.var = tk.IntVar(self.master, value = self.color)
self.label = tk.Label(self.master, anchor = tk.E)
self.label.grid(row = 0, column = 0, sticky = tk.NSEW)
self.clear() # define memory and counter
self.scroll = tk.Scale(
self.master, orient = "horizontal", resolution = 65793,
label = "Brightness Control", from_ = 0, to = 16777215,
variable = self.var, command = self.control)
self.scroll.grid(row = 1, column = 0, columnspan = 2, sticky = tk.EW)
for k, v in [
( "<Control-z>", self.undo ),
( "<Control-Z>", self.redo ),
( "<Control-x>", self.clear )]:
self.master.bind(k, v)
self.master.geometry("400x86")
self.master.update()
self.master.minsize(329, 86)
self.master.resizable(True, False)
self.master.deiconify()
def display(self):
col = "#" + ("000000" + hex(self.color)[2:])[~5:]
self.var.set(self.color)
self.label["text"] = col
self.master.tk_setPalette(col)
def control(self, n):
self.color = int(n)
self.display()
if self.color not in self.memory:
self.memory.append(self.color)
self.counter = self.counter + 1
def action(self):
self.color = self.memory[self.counter]
self.display()
def undo(self, ev = None):
if self.memory:
self.counter = max(0, self.counter - 1)
self.action()
def redo(self, ev = None):
if self.memory:
self.counter = min(len( self.memory ) - 1, self.counter + 1)
self.action()
def clear(self, ev = None):
self.memory, self.counter = [], 0
self.label["text"] = "cleared"
if __name__ == "__main__":
bright = UndoRedo()
bright.master.mainloop()

If I understand correctly, you could store all positions taken by the slider in a list and use an index pointer to manipulate where it is in the list.
silder_ind = 0
slider_positions = [4] # Or wherever you want to start by default
When the user changes the position of the slider
new_pos = slider.get_current_value() # whatever the appropriate API call is to get the current value
slider_positions.append(new_pos)
slider_ind += 1
When the user hits undo
if (slider_ind - 1) >= 0:
slider_ind -= 1
redo
if (slider_ind + 1) < len(slider_positions):
slider_ind += 1

Related

How can I put my timer into a label in another file(in my game)?

I am programming a minesweeper and I am stuck because I still have to add a timer. I was able to program one but I can only see it in my console. But i want it to be in my game so that someone who is playong can see the time.
Now how do I add my programmed timer to the game? Down here is my code the first one is for the cell with all the definitions the second one is the main part and the last part is my time. I still have two files called settings and utils but these aren‘t important for the problem…
from tkinter import Button, Label
import random
import settings
import ctypes
import sys
import time
import elapsed
class Cell:
all = []
cell_count = settings.CELL_COUNT
cell_count_label_object = None
def __init__(self,x, y, is_mine=False):
self.is_mine = is_mine
self.is_opened = False
self.is_mine_candidate = False
self.cell_btn_object = False
self.x = x
self.y = y
self.done = False
# Append the object to the Cell.all list
Cell.all.append(self)
def create_btn_object(self, location):
btn = Button(
location,
width=10,
height=3,
)
btn.bind('<Button-1>', self.left_click_actions ) # Left Click
btn.bind('<Button-3>', self.right_click_actions ) # Right Click
self.cell_btn_object = btn
#staticmethod
def create_cell_count_label(location):
lbl = Label(
location,
bg='aquamarine3',
fg='white',
text=f"Cells Left:{Cell.cell_count}",
font=("", 30)
)
Cell.cell_count_label_object = lbl
def left_click_actions(self, event):
if self.is_mine:
self.show_mine()
else:
if self.surrounded_cells_mines_length == 0:
for cell_obj in self.surrounded_cells:
cell_obj.show_cell()
self.show_cell()
# If Mines count is equal to the cells left count, player won
if Cell.cell_count == settings.MINES_COUNT:
ctypes.windll.user32.MessageBoxW(0, 'Congratulations! You won the game!', 'Game Over', 0)
# Cancel Left and Right click events if cell is already opened:
self.cell_btn_object.unbind('<Button-1>')
self.cell_btn_object.unbind('<Button-3>')
def get_cell_by_axis(self, x,y):
# Return a cell object based on the value of x,y
for cell in Cell.all:
if cell.x == x and cell.y == y:
return cell
#property
def surrounded_cells(self):
cells = [
self.get_cell_by_axis(self.x - 1, self.y -1),
self.get_cell_by_axis(self.x - 1, self.y),
self.get_cell_by_axis(self.x - 1, self.y + 1),
self.get_cell_by_axis(self.x, self.y - 1),
self.get_cell_by_axis(self.x + 1, self.y - 1),
self.get_cell_by_axis(self.x + 1, self.y),
self.get_cell_by_axis(self.x + 1, self.y + 1),
self.get_cell_by_axis(self.x, self.y + 1)
]
cells = [cell for cell in cells if cell is not None]
return cells
#property
def surrounded_cells_mines_length(self):
counter = 0
for cell in self.surrounded_cells:
if cell.is_mine:
counter += 1
return counter
def show_cell(self):
if not self.is_opened:
Cell.cell_count -= 1
self.cell_btn_object.configure(text=self.surrounded_cells_mines_length)
# Replace the text of cell count label with the newer count
if Cell.cell_count_label_object:
Cell.cell_count_label_object.configure(
text=f"Cells Left:{Cell.cell_count}"
)
# If this was a mine candidate, then for safety, we should
# configure the background color to SystemButtonFace
self.cell_btn_object.configure(
bg='SystemButtonFace'
)
# Mark the cell as opened (Use is as the last line of this method)
self.is_opened = True
def show_mine(self):
self.cell_btn_object.configure(bg='red')
ctypes.windll.user32.MessageBoxW(0, 'You clicked on a mine', 'Game Over', 0)
sys.exit()
def right_click_actions(self, event):
if not self.is_mine_candidate:
self.cell_btn_object.configure(
text = '🌸',
fg = 'red'
)
self.is_mine_candidate = True
else:
self.cell_btn_object.configure(
bg='SystemButtonFace'
)
self.is_mine_candidate = False
def time():
start_time = time.time()
while True:
if not self.is_opened:
elapsed_time = time.time() - start_time
print(int(elapsed_time))
#staticmethod
def randomize_mines():
picked_cells = random.sample(
Cell.all, settings.MINES_COUNT
)
for picked_cell in picked_cells:
picked_cell.is_mine = True
def __repr__(self):
return f"Cell({self.x}, {self.y})"
from tkinter import *
from cell import Cell
import settings
import utils
import elapsed
import time
root = Tk()
# Override the settings of the window
root.configure(bg="aquamarine3")
root.geometry(f'{settings.WIDTH}x{settings.HEIGHT}')
root.title("Minesweeper Game")
root.resizable(False, False)
top_frame = Frame(
root,
bg='aquamarine3',
width=settings.WIDTH,
height=utils.height_prct(15)
)
top_frame.place(x=0, y=0)
game_title = Label(
top_frame,
bg='aquamarine3',
fg='white',
text='Minesweeper Game',
font=('', 48)
)
game_title.place(
x=utils.width_prct(30), y=0
)
left_frame = Frame(
root,
bg='aquamarine3',
width=utils.width_prct(75),
height=utils.height_prct(75)
)
left_frame.place(x=0, y=utils.height_prct(25))
timer_title = Label(
left_frame,
bg = 'aquamarine3',
fg = 'white',
text = ('time:', command = cell.time)
)
timer_title.place(
x = 0, y = 100
)
center_frame = Frame(
root,
bg='aquamarine3',
width=utils.width_prct(75),
height=utils.height_prct(75)
)
center_frame.place(
x=utils.width_prct(25),
y=utils.height_prct(15),
)
for x in range(settings.GRID_SIZE):
for y in range(settings.GRID_SIZE):
c = Cell(x, y)
c.create_btn_object(center_frame)
c.cell_btn_object.grid(
column=x, row=y
)
# Call the label from the Cell class
Cell.create_cell_count_label(left_frame)
Cell.cell_count_label_object.place(
x=0, y=0
)
Cell.randomize_mines()
# Run the window
root.mainloop()
import time
time.gmtime(0)
start_time = time.time()
def timer():
while True:
elapsed_time = time.time()-start_time
print(int(elapsed_time))

How to create a brightness toggle on Python Tkinter

I am creating a brightness toggle on Python. I am using the Tkinter module to create a sider function. I have successfully created a slider to increase/decrease the volume. However, I want to translate this into one where I can increase/decrease the screen's brightness. Here is my volume code and output.
I am currently using macOS Monterey Version 12.2.1.
#The GUI player
import pygame
from pygame import mixer
from tkinter import *
# Makes a new window and stores it inside the root variable.
root = Tk()
mixer.init() # initialising the mixer
# Coding the background music
def play_music():
pygame.mixer.music.load("ArcadeMusic copy.mp3")
pygame.mixer.music.play(-1)
def set_vol(val):
volume = int(val) / 100
mixer.music.set_volume(volume)
root.geometry('300x70')
root.title("Volume")
text = Label(root, text = "Drag the slider to adjust volume")
text.pack()
scale = Scale(root,from_=0,to=100 , orient=HORIZONTAL, command=set_vol)
scale.pack()
play_music()
root.mainloop()
Are you trying to achieve something like this? Or do you want to control desktop brightness?
import tkinter
def find( obj ):
name = f"{type(obj).__name__}:n "
try:
return name + "n ".join( [ f"{x} = '{obj.cget(x)}'" for x in obj.keys() ] )
except:
return f"'{obj}' has no keys attribute"
class brightness:
def __init__( self ):
self.master = tkinter.Tk()
self.master.update_idletasks()
self.color = 15790320 # #f0f0f0 = SystemButtonFace
self.var = tkinter.IntVar( self.master, value = self.color )
self.flexx( self.master )
self.label = tkinter.LabelFrame(
self.master, labelanchor = 'n', text = self.master['background'] )
self.label.grid( row=0, column=0, columnspan=2, sticky='nsew' )
self.flexx( self.label, r=None )
self.flexx( self.label, r=None , c=1 )
self.scroll = tkinter.Scale(
self.label, orient = 'horizontal',
resolution = 65793, label = 'Brightness Control',
from_ = 0, to = 16777215, variable = self.var,
command = self.control )
self.scroll.grid( row = 1, column=0, columnspan=2, sticky='ew' )
self.master.geometry( '200x200' )
self.master.minsize( 334, 113 )
def flexx( self, o, r = 0, c = 0, rw = 1, cw = 1 ):
if r != None:
o.rowconfigure( r, weight = rw )
if c != None:
o.columnconfigure( c, weight = cw )
def convert( self ):
col = '#' + ( '000000' + hex( self.color )[2:])[~5:]
self.var.set( self.color )
self.label['text'] = col
self.master.tk_setPalette( col )
def control_up( self ):
self.color += 65793
if self.color > 16777215:
self.color = 15790320
col = self.convert( )
def control_down( self ):
self.color -= 65793
if self.color < 0: # 15790320:
self.color = 16777215
col = self.convert( )
def control( self, n ):
self.color = int( n )
col = self.convert( )
if __name__ == '__main__':
bright = brightness( )
tkinter.mainloop()

Improving tkinter.ttk code

I am a fairly novice programmer and through coffee, google, and an immense loss of hair and fingernails have managed to write a very messy code. I am asking anyone to help me simplify the code if possible.
from tkinter import ttk
from tkinter import *
from tkinter.ttk import *
one = 0
why = 'Total Number: {}'
no = 0
clack = 0
click = 'Clicks: {}'
s = ttk.Style()
s.theme_use('clam')
s.configure('red.Vertical.TProgressbar', foreground = 'red', background = 'red')
s.configure('green.Vertical.TProgressbar', foreground = 'green', background = 'green')
s.configure('TButton', relief = 'groove')
def iround(x):
y = round(x) - .5
return int(y) + (y > 0)
class Application(ttk.Frame):
def __init__(self, master = None):
ttk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def Number(self):
global one
cost = 10*(self.w['to'])
if self.number['text'] == "Make Number go up":
self.number['text'] = one
if iround(self.w.get()) == 0:
one += 1
else:
one += iround(self.w.get())
self.number['text'] = one
if self.number['text'] >= cost:
self.buy['state'] = 'normal'
else:
self.buy['state'] = 'disabled'
self.number['text'] >= cost
self.progress['value'] = one
if self.number['text'] >= cost:
self.progress['style'] = 'red.Vertical.TProgressbar'
else:
self.progress['style'] = 'green.Vertical.TProgressbar'
def Buy(self):
global one
self.w['to'] += 1
cost = 10*(self.w['to'])
one = self.number['text'] = (one + 10 - cost)
self.buy['text'] = ('+1 to slider | Cost: {}'.format(cost))
if self.number['text'] < (cost):
self.buy['state'] = 'disabled'
self.progress['value'] = one
self.progress['maximum'] += 10
if self.number['text'] >= cost:
self.progress['style'] = 'red.Vertical.TProgressbar'
else:
self.progress['style'] = 'green.Vertical.TProgressbar'
def scaleValue(self, event):
self.v['text'] = 'Slider Bonus + ' + str(iround(self.w.get()))
def clicks(self, event):
global click
global clack
if self.Clicks['text'] == 'Clicks: 0':
clack += 1
self.Clicks['text'] = click.format(clack)
self.Clicks['text'] = click.format(clack)
clack += 1
def NumberVal(self, event):
global why
global no
if self.fun['text'] == "Total Number: 0":
self.fun['text'] = why.format(no)
if iround(self.w.get()) == 0:
no += 1
else:
no += iround(self.w.get())
self.fun['text'] = why.format(no)
def createWidgets(self):
self.number = Button(self, text = 'Make number go up', command = self.Number, width = 20, style = 'TButton')
self.number.grid(row = 1, column = 1)
self.number.bind('<ButtonRelease>', self.clicks, add = '+')
self.number.bind('<ButtonRelease>', self.NumberVal, add = '+')
self.buy = Button(self, text = '+1 to Slider | Cost: 10', command = self.Buy, width = 20, style = 'TButton')
self.buy.grid(row = 2, column = 1)
self.buy.config(state = 'disabled')
self.v = Label(self, text = 'Slider Bonus + 0', width = 20, anchor = 'center')
self.v.grid(row = 3, column = 3)
self.w = Scale(self, from_ = 0, to = 1, orient = 'horizontal')
self.w.grid(row = 3, column = 1)
self.w.bind('<Motion>', self.scaleValue)
self.progress = Progressbar(self, value = 0, orient = 'vertical', maximum = 10, mode = 'determinate')
self.progress.grid(row = 1, rowspan = 5, column = 2)
self.Clicks = Label(self, text = 'Clicks: 0', width = 20, anchor = 'center')
self.Clicks.grid(row = 1, column = 3)
self.fun = Label(self, text = 'Total Number: 0', width = 20, anchor = 'center')
self.fun.grid(row = 2, column = 3)
app = Application()
app.master.title('Number')
app.mainloop()

Using Tkinter to program an application need add buttons with same function. How to identify which button I clicked and change the line automatically?

The following things I want is to add multiple different tasks, and there is a task start button which is used to trig a timer.
class Pomodoro_app(Tk):
def add_task(self):
global time
time = StringVar()
time.set("Start")
task_content = askstring(title = 'Add a Task', prompt = "Input a task")
task_label = Label(self, text = task_content, font = ("Arial, 12")).grid(column = 0, row = 3)
task_start_button = Button(self, textvariable = time, command = self.start_working).grid(column = 1, row = 3)
def createWidgets(self):
self.welcome_label = Label(self, text = "Welcome to the Challenge!", font = ("Arial, 12")).grid(column = 0, row = 0, columnspan = 2)
self.add_task_button = Button(self, text = "Add Task", width = 20, command = self.add_task).grid(column = 0 , columnspan = 2)
def __init__(self):
"""GUI Initiation"""
Tk.__init__(self)
self.createWidgets()
"""window Initiation"""
self.resizable(False, False)
x = (self.winfo_screenwidth() - self.winfo_reqwidth()) / 2
y = (self.winfo_screenheight() - self.winfo_reqheight()) / 2
self.geometry('250x400+%d+%d' % (x, y))
Using lambda (or inner function), you can pass additional argument to callback.
For example:
class Pomodoro_app(Tk):
def add_task(self):
global time
time = StringVar()
time.set("Start")
task_content = askstring(title = 'Add a Task', prompt = "Input a task")
task_label = Label(self, text = task_content, font = ("Arial, 12")).grid(column = 0, row = self.next_row)
task_start_button = Button(self, textvariable = time, command = lambda: self.start_working(task_content)).grid(column = 1, row = self.next_row)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
self.next_row += 1
def start_working(self, task_name):
print('start working', task_name)
def createWidgets(self):
self.welcome_label = Label(self, text = "Welcome to the Challenge!", font = ("Arial, 12")).grid(column = 0, row = 0, columnspan = 2)
self.add_task_button = Button(self, text = "Add Task", width = 20, command = self.add_task).grid(column = 0 , columnspan = 2)
def __init__(self):
"""GUI Initiation"""
Tk.__init__(self)
self.createWidgets()
"""window Initiation"""
self.resizable(False, False)
x = (self.winfo_screenwidth() - self.winfo_reqwidth()) / 2
y = (self.winfo_screenheight() - self.winfo_reqheight()) / 2
self.geometry('250x400+%d+%d' % (x, y))
self.next_row = 3

how to update a listbox in one window in one class using a different class python

what i have is a window that opens up and it has a list box. this is created using one class. when i click the search button and results are found using a different class, i want the list box to update without having to open up another window. below is my code so far\n
from Tkinter import *
class addTask:
def __init__(self):
self.addWindow = Tk()
self.addWindow.configure(background = "black")
self.addWindow.geometry("450x450")
self.addWindow.resizable(width = False, height = False)
self.addWindow.title("Add Task")
self.addNameLabel = Label(self.addWindow,text="Add the name of the task",font = ("Helvetica",10,"italic"),bg = "black",fg = "white")
self.addNameLabel.place(relx=0.01, rely=0.05)
self.nameWiget = Text (self.addWindow, width = 63, height = 1)
self.nameWiget.place(relx=0.0, rely=0.1)
self.addDateLabel = Label(self.addWindow,text="Add the date of the task",font = ("Helvetica",10,"italic"),bg = "black",fg = "white")
self.addDateLabel.place(relx=0.01, rely=0.2)
self.dateWiget = Text (self.addWindow, width = 63, height = 1)
self.dateWiget.place(relx=0.0, rely=0.25)
self.addTaskLabel = Label(self.addWindow,text="Add the details of the task",font = ("Helvetica",10,"italic"),bg = "black",fg = "white")
self.addTaskLabel.place(relx=0.01, rely=0.35)
self.taskWiget = Text (self.addWindow, width = 63, height = 1)
self.taskWiget.place(relx=0.0, rely=0.4)
addButton = Button(self.addWindow,height = 5, width = 20, text="Add Task",highlightbackground="black",font=("Helvetica",10,"bold"),command=lambda:self.saveFuntion())
addButton.place(relx=0.25, rely=0.55)
def saveFuntion(self):
nameInfo = (self.nameWiget.get(1.0, END))
dateInfo = self.dateWiget.get(1.0, END)
taskInfo = self.taskWiget.get(1.0, END)
print nameInfo
task1 = Task(nameInfo,dateInfo,taskInfo)
task1.save()
self.nameWiget.delete(1.0,END)
class Task:
def __init__(self,name,date,task):
self.__name = name
self.__date = date
self.__task = task
def save(self):
fileName = open("dataFile.txt","a")
fileName.write(self.__name)
fileName.write(self.__date)
fileName.write(self.__task)
class editTask:
def __init__(self):
self.editWindow = Tk()
self.newWindow = Tk()
self.newWindow.geometry("450x750")
self.editWindow.configure(background = "black")
self.editWindow.geometry("450x750")
self.editWindow.resizable(width = False, height = False)
self.editWindow.title("Edit Task")
self.listBox = Listbox(self.editWindow,heigh = 15, width = 30)
self.listBox.place(relx = 0.2, rely = 0.6)
#drop down menu
self.var = StringVar(self.editWindow)
self.var.set("Select search critria")
self.choices = ["Name","Date"]
self.option = OptionMenu(self.editWindow,self.var,*self.choices)
self.option.configure(bg = "black")
self.option.place(relx = 0.5, rely = 0.2)
#edit label and text box
self.editLabel = Label(self.editWindow,text="Add the name of the task",font = ("Helvetica",10,"italic"),bg = "black",fg = "white")
self.editLabel.place(relx=0.01, rely=0.05)
self.editInfoWiget = Text (self.editWindow, width = 63, height = 1)
self.editInfoWiget.place(relx=0.0, rely=0.1)
# search button
searchButton = Button(self.editWindow,height = 5, width = 20, text="Search for Task",highlightbackground="black",font=("Helvetica",10,"bold"),command=lambda:self.searchFuntion())
searchButton.place(relx=0.3, rely=0.4)
def searchFuntion(self):
critria = self.var.get()
info = self.editInfoWiget.get(1.0,END)
thing = info.split("\n")
thing2 = thing[0]
search = searchCritria(critria,thing2)
search.search()
# def openListBox(self):
class searchCritria():
def __init__(self,critria,info):
self.__critria = critria
self.__info = info
def search(self):
self.file = open("dataFile.txt", "r+")
fileData = self.file.readlines()
self.file.close()
lengthOfFile = len(fileData)
counter = 1
self.name = []
self.date = []
self.details = []
for i in range (lengthOfFile):
split = fileData[i].split("\n")
while counter == 1:
self.name.append(split)
break
while counter == 2:
self.date.append(split)
break
while counter == 3:
self.details.append(split)
break
counter = counter +1
if counter > 3:
counter = 1
if self.__critria == "Name":
for x in range (len(self.name)):
self.taskName = self.name[x]
self.taskName2 = self.taskName[0]
if self.__info == self.taskName2:
openWindow = True
else :
openWindow = False
if openWindow == True:
editTask().listBox.insert(END,self.taskName2)
if self.__critria == "Date":
for x in range (len(self.date)):
self.taskDate = self.date[x]
self.taskDate2 = self.taskDate[0]
if self.__info == self.taskDate2:
print "found"
else :
print"not found"
class editTask2():
def __init__(self):
self.edit2Window = Tk()
self.edit2Window.configure(background = "black")
self.edit2Window.geometry("450x350")
self.edit2Window.resizable(width = False, height = False)
self.edit2Window.title("Edit Task")
any help would be great
thanks
The biggest problem in your code is that you're creating multiple instances of Tk. Tkinter simply isn't designed to work that way. You should only ever create a single instance of Tk. If you need additional windows, create instances of Toplevel.
Also, you need to call the mainloop function of this instance of Tk exactly once. Without it, your GUI will not work.
If you want to update a listbox in another class than where it was created, the concept is pretty simple: if A needs to update B, A needs a reference to B. So, either pass in a reference to the listbox when you create the other class, or give the other class a method you can call where you pass in the reference to the listbox after it was created.

Categories

Resources