英文:
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"变量。
这是否可能实现?
按下"faster"按钮后。
以下是代码。
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'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?
[![Initial run][1]][1]
After pressing the "faster" button.
[![After pressing faster button][2]][2]
Here'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"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()
And here'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 ofCTkEntry
. Use separate variables instead. - create new instance of
CTkEntry
insidebutton_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"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)
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论