英文:
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"变量。
这个可能行得通吗?
按下"更快"按钮后。
以下是代码。
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?
After pressing the "faster" button.
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.
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 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)
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论