如何对滑块比例值进行撤消和重做?

2024-10-04 03:24:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码如下:

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

我想当我点击该按钮时,我会使滑块值撤消或重做


Tags: to代码fromselfvarrootwidthvariable
2条回答

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

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

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

Scale的更改使用名为计数器的索引int存储在名为内存的列表中

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

相关问题 更多 >