如何使撤消和重做滑块规模值?



我的代码如下:

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)

按钮UNDO和另一个按钮REDO

我想当我点击那个按钮时我让滑块值撤消或重做

此代码允许使用Control键更改tkinter.tk窗口的master颜色。

Scale的修改使用索引int(counter)存储在称为内存的列表中。

Control-z将撤销

Control-Z将重做

Control-x将清除内存

根据你的需要修改它应该不会太难。

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()

如果我理解正确的话,您可以将滑块所取的所有位置存储在列表中,并使用索引指针来操作它在列表中的位置。

silder_ind = 0
slider_positions = [4] # Or wherever you want to start by default

当用户改变滑块的位置时

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

当用户点击undo

if (slider_ind - 1) >= 0:
slider_ind -= 1

重做

if (slider_ind + 1) < len(slider_positions):
slider_ind += 1

最新更新