在程序执行期间更改customtkinter CTKEntry占位符中的值

问题描述 投票:0回答:1

我正在 PyCharm 中开发一个程序,该程序将允许用户在 ctkENTRY 框中输入值或按下按钮来更改该值。我可以在程序运行时使用变量来设置占位符值,但如果用户决定按按钮,我希望它能够更新。我刚刚开始对按钮进行编码,现在只对电机速度按钮(“更快”和“更慢”)进行编码。我尝试用谷歌搜索答案,但找不到任何东西,所以我尝试将以下行放入buttonNum 1的button_event代码中

self.entry_speed = customtkinter.CTkEntry(self.frame, placeholder_text=speed)

但这似乎并没有影响任何事情。

我确实通过打印出变量并观察它在调试模式下的变化来验证代码正在更改“速度”变量。

这可能吗?

按下“更快”按钮后。

这是代码。

from configparser import ConfigParser
import sys
import tkinter
import tkinter.messagebox
import customtkinter

customtkinter.set_appearance_mode("System")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"

# instantiate
config = ConfigParser()

# parse existing file
config.read('config.ini')

# read values from a section
speed = int(config.get('settings', 'speed'))
direction = config.get('settings', 'direction')
feed_steps = config.getint('settings', 'feed_steps')

print(f"current settings")
print(f"speed set to", speed)
print(f"Motor direction is ", direction)
print(f"Feeding steps is ", feed_steps)


class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        global speed
        global direction
        global feed_steps


        #   Begin row count
        r = 0

        #   Set column to 0
        c = 0

        #   Set direction
        current_direction = f"Clockwise"

        #   Set steps per feeding
        current_steps_feeding = 200

        # configure window
        self.title("Motor Options")
        self.geometry(f"{750}x{385}")

        # configure grid layout (4x4)
        self.grid_columnconfigure((0, 1, 2, 3), weight=1)

        # create sidebar frame with widgets
        self.frame = customtkinter.CTkFrame(self, width=750, corner_radius=0)
        self.frame.grid(row=0, column=c, rowspan=3, sticky="nsew")
        #        self.frame.grid_rowconfigure(9, weight=1)

        self.logo_label = customtkinter.CTkLabel(self.frame, text="Motor Options",
                                                 font=customtkinter.CTkFont(size=20, weight="bold"))
        self.logo_label.grid(row=r, column=c, columnspan=4, padx=20, pady=(10, 10))

        r += 1
        self.logo_label = customtkinter.CTkLabel(self.frame, text="Speed",
                                                 font=customtkinter.CTkFont(size=20, weight="bold"))
        self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Speed up
        c += 1
        self.button_1 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(1))
        self.button_1.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Speed down
        c += 1
        self.button_2 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(2))
        self.button_2.grid(row=r, column=c, padx=20, pady=(10, 10))

        # create Speed entry box
        c += 1
        self.entry_speed = customtkinter.CTkEntry(self.frame, placeholder_text=speed)
        self.entry_speed.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Direction
        c = 0
        r += 1
        self.logo_label = customtkinter.CTkLabel(self.frame, text="Direction",
                                                 font=customtkinter.CTkFont(size=20, weight="bold"))
        self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Clockwise
        c += 1
        self.button_3 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(3))
        self.button_3.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Counter Clockwise
        c += 1
        self.button_4 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(4))
        self.button_4.grid(row=r, column=c, padx=20, pady=(10, 10))

        # create direction entry
        c += 1
        self.entry_direction = customtkinter.CTkEntry(self.frame, placeholder_text=direction)
        self.entry_direction.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Steps per Feeding
        c = 0
        r += 1
        self.logo_label = customtkinter.CTkLabel(self.frame, text="Steps per Feeding",
                                                 font=customtkinter.CTkFont(size=20, weight="bold"))
        self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Steps per feed +100
        c += 1
        self.button_5 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(5))
        self.button_5.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Steps per feed +10
        c += 1
        self.button_6 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(6))
        self.button_6.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Steps per feed +1
        c += 1
        self.button_7 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(7))
        self.button_7.grid(row=r, column=c, padx=20, pady=(10, 10))

        # create steps per feeding box
        c = 0
        r += 1
        self.entry_speed = customtkinter.CTkEntry(self.frame, placeholder_text=feed_steps)
        self.entry_speed.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Steps per feed -100
        c += 1
        self.button_8 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(8))
        self.button_8.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Steps per feed -10
        c += 1
        self.button_9 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(9))
        self.button_9.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Steps per feed -1
        c += 1
        self.button_10 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(10))
        self.button_10.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Test
        c = 0
        r += 1
        self.logo_label = customtkinter.CTkLabel(self.frame, text="Test",
                                                 font=customtkinter.CTkFont(size=20, weight="bold"))
        self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Test feed
        c += 1
        self.button_11 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(11))
        self.button_11.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Run motor
        c += 1
        self.button_12 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(12))
        self.button_12.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Stop motor
        c += 1
        self.button_13 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(13))
        self.button_13.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Save/Reset/Cancel
        c = 0
        r += 1
        self.logo_label = customtkinter.CTkLabel(self.frame, text="",
                                                 font=customtkinter.CTkFont(size=20, weight="bold"))
        self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Save changes
        r += 1
        c += 1
        self.button_14 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(14))
        self.button_14.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Reset
        c += 1
        self.button_15 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(15))
        self.button_15.grid(row=r, column=c, padx=20, pady=(10, 10))

        #   Cancel
        c += 1
        self.button_16 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(16))
        self.button_16.grid(row=r, column=c, padx=20, pady=(10, 10))

        self.button_1.configure(text="Faster")
        self.button_2.configure(text="Slower")
        self.button_3.configure(text="CW")
        self.button_4.configure(text="CCW")
        self.button_5.configure(text="+100")
        self.button_6.configure(text="+10")
        self.button_7.configure(text="+1")
        self.button_8.configure(text="-100")
        self.button_9.configure(text="-10")
        self.button_10.configure(text="-1")
        self.button_11.configure(text="Feed Test")
        self.button_12.configure(text="Run Motor")
        self.button_13.configure(text="Stop Motor")
        self.button_14.configure(text="Save")
        self.button_15.configure(text="Reset")
        self.button_16.configure(text="Cancel")

    def button_event(self, buttonNum):
        global speed

        if buttonNum == 1:
            print(f"Faster")
            if 0 <= speed < 100:
                speed += 1
                print(speed)
                self.entry_speed = customtkinter.CTkEntry(self.frame, placeholder_text=speed)
        elif buttonNum == 2:
            print(f"Slower")
            if speed > 0 and speed < 101:
                speed -= 1
                print(speed)
        elif buttonNum == 3:
            print(f"CW")
        elif buttonNum == 4:
            print(f"CCW")
        elif buttonNum == 5:
            print(f"+100")
        elif buttonNum == 6:
            print(f"+10")
        elif buttonNum == 7:
            print(f"+1")
        elif buttonNum == 8:
            print(f"-100")
        elif buttonNum == 9:
            print(f"-10")
        elif buttonNum == 10:
            print(f"-1")
        elif buttonNum == 11:
            print(f"Feed Test")
        elif buttonNum == 12:
            print(f"Run Motor")
        elif buttonNum == 13:
            print(f"Stop Motor")
        elif buttonNum == 14:
            print(f"Save")
        elif buttonNum == 15:
            print(f"Reset")
        elif buttonNum == 16:
            print(f"Cancel")
            sys.exit(0)


if __name__ == "__main__":
    app = App()
    app.mainloop()

这是 config.ini 文件。

speed: 10
direction = CW
feed_steps: 150
python tkinter tkinter-entry customtkinter
1个回答
0
投票

您的代码中存在以下问题:

  • 同一个变量
    self.entry_speed
    用于
    CTkEntry
    的两个实例。请改用单独的变量。
  • CTkEntry
    内创建
    button_event()
    的新实例。使用
    .configure()
    来更新现有小部件。
class App(customtkinter.CTk):
    def __init__(self):
        ...
        # create steps per feeding box
        c = 0
        r += 1
        # use another instance variable for this entry
        self.entry_feeding = customtkinter.CTkEntry(self.frame, placeholder_text=feed_steps)
        self.entry_feeding.grid(row=r, column=c, padx=20, pady=(10, 10))
        ...

    def button_event(self, buttonNum):
        global speed

        if buttonNum == 1:
            print(f"Faster")
            if 0 <= speed < 100:
                speed += 1
                print(speed)
                # use .configure() to update widget option
                self.entry_speed.configure(placeholder_text=speed)
        elif buttonNum == 2:
            print(f"Slower")
            if speed > 0 and speed < 101:
                speed -= 1
                print(speed)
        ...
© www.soinside.com 2019 - 2024. All rights reserved.