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

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

Changing the value in customtkinter CTKEntry placeholder during program execution

问题

我正在使用PyCharm编写一个程序,允许用户要么在ctkENTRY框中输入一个值,要么按下按钮来更改该值。当程序运行时,我可以使用一个变量来设置占位符的值,但如果用户决定按下按钮而不是输入值,我希望它能更新。我目前只编写了电机速度按钮("faster"和"slower"),正在编写按钮。我尝试了谷歌搜索答案,但找不到任何答案,所以我尝试将以下行放入buttonNum 1的button_event代码中:

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

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

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

这是否可能实现?

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

按下"faster"按钮后。

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

以下是代码。

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

customtkinter.set_appearance_mode("System")  # 模式: "System" (标准), "Dark" (暗), "Light" (亮)
customtkinter.set_default_color_theme("blue")  # 主题: "blue" (标准), "green" (绿色), "dark-blue" (深蓝色)

# 实例化
config = ConfigParser()

# 解析现有文件
config.read('config.ini')

# 从一个部分读取值
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


        # 开始行计数
        r = 0

        # 设置列为0
        c = 0

        # 设置方向
        current_direction = "Clockwise"

        # 设置每次供料的步数
        current_steps_feeding = 200

        # 配置窗口
        self.title("Motor Options")
        self.geometry(f"{750}x{385}")

        # 配置网格布局(4x4)
        self.grid_columnconfigure((0, 1, 2, 3), weight=1)

        # 创建带有小部件的侧边栏框架
        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))

        # 速度增加
        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))

        # 速度减少
        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))

        # 创建速度输入框
        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))

        # 方向
        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))

        # 顺时针
        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))

        # 逆时针
        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))

        # 创建方向输入
        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))

        # 每次供料的步数
        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))

        # 每次供料+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))

        # 每次供料+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))

        # 每次供料+1
        c += 1
        self.button_7 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(7))
        self.button_7.grid(row=r, column=c

<details>
<summary>英文:</summary>

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&#39;m just starting to code the buttons and right now only have the motor speed buttons (&quot;faster&quot; and &quot;slower&quot;) coded.  I tried to google an answer, but can&#39;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&#39;t seem to affect anything.

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

Is this even possible to do?

[![Initial run][1]][1]

After pressing the &quot;faster&quot; button.

[![After pressing faster button][2]][2]

Here&#39;s the code.

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&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 == "main":
app = App()
app.mainloop()


And here&#39;s the config.ini file.
```[settings]
speed: 10
direction = CW
feed_steps: 150

答案1

得分: 1

你的代码存在以下问题:

  • 在两个CTkEntry实例中都使用了相同的变量self.entry_speed。应该使用不同的变量来代替。
  • 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"Faster")
            if 0 <= speed < 100:
                speed += 1
                print(speed)
                # 使用.configure()来更新小部件的选项
                self.entry_speed.configure(placeholder_text=speed)
        elif buttonNum == 2:
            print(f"Slower")
            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-2.html
匿名

发表评论

匿名网友

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

确定