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

huangapple go评论79阅读模式
英文:

Changing the value in customtkinter CTKEntry placeholder during program execution

问题

我正在使用PyCharm编写一个程序,允许用户在ctkENTRY框中输入一个值,或者按下按钮来改变该值。当程序运行时,我可以使用一个变量来设置占位符的值,但是如果用户决定按下按钮,我希望它能更新。我正在编写按钮的代码,目前只编写了电机速度按钮("更快"和"更慢")。我尝试在button_event代码中为buttonNum 1添加以下行:

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

但是似乎没有影响。

我通过打印变量并在调试模式下观察变量的变化来验证代码是否更改了"speed"变量。

这个可能行得通吗?

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

按下"更快"按钮后。

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

以下是代码。

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文件。

[settings]
speed: 10
direction = CW
feed_steps: 150
英文:

I am working on a program in PyCharm that will allow a user to either enter a value in a ctkENTRY box or press a button(s) to change the value. I can use a variable to set the placeholder value when the program runs, but I would like it to update if the user decides to press a button instead. I'm just starting to code the buttons and right now only have the motor speed buttons ("faster" and "slower") coded. I tried to google an answer, but can't find anything, so I tried putting the following line in the button_event code for buttonNum 1

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

But that didn't seem to affect anything.

I did verify that the code is changing the "speed" variable by printing out the variable and observing it change in debug mode.

Is this even possible to do?

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

After pressing the "faster" button.

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

Here's the code.

from configparser import ConfigParser
import sys
import tkinter
import tkinter.messagebox
import customtkinter
customtkinter.set_appearance_mode(&quot;System&quot;)  # Modes: &quot;System&quot; (standard), &quot;Dark&quot;, &quot;Light&quot;
customtkinter.set_default_color_theme(&quot;blue&quot;)  # Themes: &quot;blue&quot; (standard), &quot;green&quot;, &quot;dark-blue&quot;
# instantiate
config = ConfigParser()
# parse existing file
config.read(&#39;config.ini&#39;)
# read values from a section
speed = int(config.get(&#39;settings&#39;, &#39;speed&#39;))
direction = config.get(&#39;settings&#39;, &#39;direction&#39;)
feed_steps = config.getint(&#39;settings&#39;, &#39;feed_steps&#39;)
print(f&quot;current settings&quot;)
print(f&quot;speed set to&quot;, speed)
print(f&quot;Motor direction is &quot;, direction)
print(f&quot;Feeding steps is &quot;, 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&quot;Clockwise&quot;
#   Set steps per feeding
current_steps_feeding = 200
# configure window
self.title(&quot;Motor Options&quot;)
self.geometry(f&quot;{750}x{385}&quot;)
# 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=&quot;nsew&quot;)
#        self.frame.grid_rowconfigure(9, weight=1)
self.logo_label = customtkinter.CTkLabel(self.frame, text=&quot;Motor Options&quot;,
font=customtkinter.CTkFont(size=20, weight=&quot;bold&quot;))
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=&quot;Speed&quot;,
font=customtkinter.CTkFont(size=20, weight=&quot;bold&quot;))
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=&quot;Direction&quot;,
font=customtkinter.CTkFont(size=20, weight=&quot;bold&quot;))
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=&quot;Steps per Feeding&quot;,
font=customtkinter.CTkFont(size=20, weight=&quot;bold&quot;))
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=&quot;Test&quot;,
font=customtkinter.CTkFont(size=20, weight=&quot;bold&quot;))
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=&quot;&quot;,
font=customtkinter.CTkFont(size=20, weight=&quot;bold&quot;))
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=&quot;Faster&quot;)
self.button_2.configure(text=&quot;Slower&quot;)
self.button_3.configure(text=&quot;CW&quot;)
self.button_4.configure(text=&quot;CCW&quot;)
self.button_5.configure(text=&quot;+100&quot;)
self.button_6.configure(text=&quot;+10&quot;)
self.button_7.configure(text=&quot;+1&quot;)
self.button_8.configure(text=&quot;-100&quot;)
self.button_9.configure(text=&quot;-10&quot;)
self.button_10.configure(text=&quot;-1&quot;)
self.button_11.configure(text=&quot;Feed Test&quot;)
self.button_12.configure(text=&quot;Run Motor&quot;)
self.button_13.configure(text=&quot;Stop Motor&quot;)
self.button_14.configure(text=&quot;Save&quot;)
self.button_15.configure(text=&quot;Reset&quot;)
self.button_16.configure(text=&quot;Cancel&quot;)
def button_event(self, buttonNum):
global speed
if buttonNum == 1:
print(f&quot;Faster&quot;)
if 0 &lt;= speed &lt; 100:
speed += 1
print(speed)
self.entry_speed = customtkinter.CTkEntry(self.frame, placeholder_text=speed)
elif buttonNum == 2:
print(f&quot;Slower&quot;)
if speed &gt; 0 and speed &lt; 101:
speed -= 1
print(speed)
elif buttonNum == 3:
print(f&quot;CW&quot;)
elif buttonNum == 4:
print(f&quot;CCW&quot;)
elif buttonNum == 5:
print(f&quot;+100&quot;)
elif buttonNum == 6:
print(f&quot;+10&quot;)
elif buttonNum == 7:
print(f&quot;+1&quot;)
elif buttonNum == 8:
print(f&quot;-100&quot;)
elif buttonNum == 9:
print(f&quot;-10&quot;)
elif buttonNum == 10:
print(f&quot;-1&quot;)
elif buttonNum == 11:
print(f&quot;Feed Test&quot;)
elif buttonNum == 12:
print(f&quot;Run Motor&quot;)
elif buttonNum == 13:
print(f&quot;Stop Motor&quot;)
elif buttonNum == 14:
print(f&quot;Save&quot;)
elif buttonNum == 15:
print(f&quot;Reset&quot;)
elif buttonNum == 16:
print(f&quot;Cancel&quot;)
sys.exit(0)
if __name__ == &quot;__main__&quot;:
app = App()
app.mainloop()

And here's the config.ini file.

speed: 10
direction = CW
feed_steps: 150

答案1

得分: 1

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

  • self.entry_speed 变量被用于两个 CTkEntry 实例。请使用不同的变量来代替。
  • button_event() 函数中创建了一个新的 CTkEntry 实例。请使用 .configure() 方法来更新现有的小部件。
class App(customtkinter.CTk):
    def __init__(self):
        ...
        # 创建每次喂食的步数输入框
        c = 0
        r += 1
        # 使用另一个实例变量来表示这个输入框
        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"更快")
            if 0 <= speed < 100:
                speed += 1
                print(speed)
                # 使用 .configure() 方法来更新小部件的选项
                self.entry_speed.configure(placeholder_text=speed)
        elif buttonNum == 2:
            print(f"更慢")
            if speed > 0 and speed < 101:
                speed -= 1
                print(speed)
        ...
英文:

There are following issues in your code:

  • same variable self.entry_speed used for two instances of CTkEntry. Use separate variables instead.
  • create new instance of CTkEntry inside button_event(). Use .configure() to update existing widget instead.
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&quot;Faster&quot;)
            if 0 &lt;= speed &lt; 100:
                speed += 1
                print(speed)
                # use .configure() to update widget option
                self.entry_speed.configure(placeholder_text=speed)
        elif buttonNum == 2:
            print(f&quot;Slower&quot;)
            if speed &gt; 0 and speed &lt; 101:
                speed -= 1
                print(speed)
        ...

huangapple
  • 本文由 发表于 2023年8月9日 10:27:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定