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

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

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占位符的值

以下是代码。

  1. from configparser import ConfigParser
  2. import sys
  3. import tkinter
  4. import tkinter.messagebox
  5. import customtkinter
  6. customtkinter.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light"
  7. customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
  8. # instantiate
  9. config = ConfigParser()
  10. # parse existing file
  11. config.read('config.ini')
  12. # read values from a section
  13. speed = int(config.get('settings', 'speed'))
  14. direction = config.get('settings', 'direction')
  15. feed_steps = config.getint('settings', 'feed_steps')
  16. print(f"current settings")
  17. print(f"speed set to", speed)
  18. print(f"Motor direction is ", direction)
  19. print(f"Feeding steps is ", feed_steps)
  20. class App(customtkinter.CTk):
  21. def __init__(self):
  22. super().__init__()
  23. global speed
  24. global direction
  25. global feed_steps
  26. # Begin row count
  27. r = 0
  28. # Set column to 0
  29. c = 0
  30. # Set direction
  31. current_direction = f"Clockwise"
  32. # Set steps per feeding
  33. current_steps_feeding = 200
  34. # configure window
  35. self.title("Motor Options")
  36. self.geometry(f"{750}x{385}")
  37. # configure grid layout (4x4)
  38. self.grid_columnconfigure((0, 1, 2, 3), weight=1)
  39. # create sidebar frame with widgets
  40. self.frame = customtkinter.CTkFrame(self, width=750, corner_radius=0)
  41. self.frame.grid(row=0, column=c, rowspan=3, sticky="nsew")
  42. # self.frame.grid_rowconfigure(9, weight=1)
  43. self.logo_label = customtkinter.CTkLabel(self.frame, text="Motor Options",
  44. font=customtkinter.CTkFont(size=20, weight="bold"))
  45. self.logo_label.grid(row=r, column=c, columnspan=4, padx=20, pady=(10, 10))
  46. r += 1
  47. self.logo_label = customtkinter.CTkLabel(self.frame, text="Speed",
  48. font=customtkinter.CTkFont(size=20, weight="bold"))
  49. self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))
  50. # Speed up
  51. c += 1
  52. self.button_1 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(1))
  53. self.button_1.grid(row=r, column=c, padx=20, pady=(10, 10))
  54. # Speed down
  55. c += 1
  56. self.button_2 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(2))
  57. self.button_2.grid(row=r, column=c, padx=20, pady=(10, 10))
  58. # create Speed entry box
  59. c += 1
  60. self.entry_speed = customtkinter.CTkEntry(self.frame, placeholder_text=speed)
  61. self.entry_speed.grid(row=r, column=c, padx=20, pady=(10, 10))
  62. # Direction
  63. c = 0
  64. r += 1
  65. self.logo_label = customtkinter.CTkLabel(self.frame, text="Direction",
  66. font=customtkinter.CTkFont(size=20, weight="bold"))
  67. self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))
  68. # Clockwise
  69. c += 1
  70. self.button_3 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(3))
  71. self.button_3.grid(row=r, column=c, padx=20, pady=(10, 10))
  72. # Counter Clockwise
  73. c += 1
  74. self.button_4 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(4))
  75. self.button_4.grid(row=r, column=c, padx=20, pady=(10, 10))
  76. # create direction entry
  77. c += 1
  78. self.entry_direction = customtkinter.CTkEntry(self.frame, placeholder_text=direction)
  79. self.entry_direction.grid(row=r, column=c, padx=20, pady=(10, 10))
  80. # Steps per Feeding
  81. c = 0
  82. r += 1
  83. self.logo_label = customtkinter.CTkLabel(self.frame, text="Steps per Feeding",
  84. font=customtkinter.CTkFont(size=20, weight="bold"))
  85. self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))
  86. # Steps per feed +100
  87. c += 1
  88. self.button_5 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(5))
  89. self.button_5.grid(row=r, column=c, padx=20, pady=(10, 10))
  90. # Steps per feed +10
  91. c += 1
  92. self.button_6 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(6))
  93. self.button_6.grid(row=r, column=c, padx=20, pady=(10, 10))
  94. # Steps per feed +1
  95. c += 1
  96. self.button_7 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(7))
  97. self.button_7.grid(row=r, column=c, padx=20, pady=(10, 10))
  98. # create steps per feeding box
  99. c = 0
  100. r += 1
  101. self.entry_speed = customtkinter.CTkEntry(self.frame, placeholder_text=feed_steps)
  102. self.entry_speed.grid(row=r, column=c, padx=20, pady=(10, 10))
  103. # Steps per feed -100
  104. c += 1
  105. self.button_8 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(8))
  106. self.button_8.grid(row=r, column=c, padx=20, pady=(10, 10))
  107. # Steps per feed -10
  108. c += 1
  109. self.button_9 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(9))
  110. self.button_9.grid(row=r, column=c, padx=20, pady=(10, 10))
  111. # Steps per feed -1
  112. c += 1
  113. self.button_10 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(10))
  114. self.button_10.grid(row=r, column=c, padx=20, pady=(10, 10))
  115. # Test
  116. c = 0
  117. r += 1
  118. self.logo_label = customtkinter.CTkLabel(self.frame, text="Test",
  119. font=customtkinter.CTkFont(size=20, weight="bold"))
  120. self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))
  121. # Test feed
  122. c += 1
  123. self.button_11 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(11))
  124. self.button_11.grid(row=r, column=c, padx=20, pady=(10, 10))
  125. # Run motor
  126. c += 1
  127. self.button_12 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(12))
  128. self.button_12.grid(row=r, column=c, padx=20, pady=(10, 10))
  129. # Stop motor
  130. c += 1
  131. self.button_13 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(13))
  132. self.button_13.grid(row=r, column=c, padx=20, pady=(10, 10))
  133. # Save/Reset/Cancel
  134. c = 0
  135. r += 1
  136. self.logo_label = customtkinter.CTkLabel(self.frame, text="",
  137. font=customtkinter.CTkFont(size=20, weight="bold"))
  138. self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))
  139. # Save changes
  140. r += 1
  141. c += 1
  142. self.button_14 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(14))
  143. self.button_14.grid(row=r, column=c, padx=20, pady=(10, 10))
  144. # Reset
  145. c += 1
  146. self.button_15 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(15))
  147. self.button_15.grid(row=r, column=c, padx=20, pady=(10, 10))
  148. # Cancel
  149. c += 1
  150. self.button_16 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(16))
  151. self.button_16.grid(row=r, column=c, padx=20, pady=(10, 10))
  152. self.button_1.configure(text="Faster")
  153. self.button_2.configure(text="Slower")
  154. self.button_3.configure(text="CW")
  155. self.button_4.configure(text="CCW")
  156. self.button_5.configure(text="+100")
  157. self.button_6.configure(text="+10")
  158. self.button_7.configure(text="+1")
  159. self.button_8.configure(text="-100")
  160. self.button_9.configure(text="-10")
  161. self.button_10.configure(text="-1")
  162. self.button_11.configure(text="Feed Test")
  163. self.button_12.configure(text="Run Motor")
  164. self.button_13.configure(text="Stop Motor")
  165. self.button_14.configure(text="Save")
  166. self.button_15.configure(text="Reset")
  167. self.button_16.configure(text="Cancel")
  168. def button_event(self, buttonNum):
  169. global speed
  170. if buttonNum == 1:
  171. print(f"Faster")
  172. if 0 <= speed < 100:
  173. speed += 1
  174. print(speed)
  175. self.entry_speed = customtkinter.CTkEntry(self.frame, placeholder_text=speed)
  176. elif buttonNum == 2:
  177. print(f"Slower")
  178. if speed > 0 and speed < 101:
  179. speed -= 1
  180. print(speed)
  181. elif buttonNum == 3:
  182. print(f"CW")
  183. elif buttonNum == 4:
  184. print(f"CCW")
  185. elif buttonNum == 5:
  186. print(f"+100")
  187. elif buttonNum == 6:
  188. print(f"+10")
  189. elif buttonNum == 7:
  190. print(f"+1")
  191. elif buttonNum == 8:
  192. print(f"-100")
  193. elif buttonNum == 9:
  194. print(f"-10")
  195. elif buttonNum == 10:
  196. print(f"-1")
  197. elif buttonNum == 11:
  198. print(f"Feed Test")
  199. elif buttonNum == 12:
  200. print(f"Run Motor")
  201. elif buttonNum == 13:
  202. print(f"Stop Motor")
  203. elif buttonNum == 14:
  204. print(f"Save")
  205. elif buttonNum == 15:
  206. print(f"Reset")
  207. elif buttonNum == 16:
  208. print(f"Cancel")
  209. sys.exit(0)
  210. if __name__ == "__main__":
  211. app = App()
  212. app.mainloop()

这是config.ini文件。

  1. [settings]
  2. speed: 10
  3. direction = CW
  4. 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.

  1. from configparser import ConfigParser
  2. import sys
  3. import tkinter
  4. import tkinter.messagebox
  5. import customtkinter
  6. customtkinter.set_appearance_mode(&quot;System&quot;) # Modes: &quot;System&quot; (standard), &quot;Dark&quot;, &quot;Light&quot;
  7. customtkinter.set_default_color_theme(&quot;blue&quot;) # Themes: &quot;blue&quot; (standard), &quot;green&quot;, &quot;dark-blue&quot;
  8. # instantiate
  9. config = ConfigParser()
  10. # parse existing file
  11. config.read(&#39;config.ini&#39;)
  12. # read values from a section
  13. speed = int(config.get(&#39;settings&#39;, &#39;speed&#39;))
  14. direction = config.get(&#39;settings&#39;, &#39;direction&#39;)
  15. feed_steps = config.getint(&#39;settings&#39;, &#39;feed_steps&#39;)
  16. print(f&quot;current settings&quot;)
  17. print(f&quot;speed set to&quot;, speed)
  18. print(f&quot;Motor direction is &quot;, direction)
  19. print(f&quot;Feeding steps is &quot;, feed_steps)
  20. class App(customtkinter.CTk):
  21. def __init__(self):
  22. super().__init__()
  23. global speed
  24. global direction
  25. global feed_steps
  26. # Begin row count
  27. r = 0
  28. # Set column to 0
  29. c = 0
  30. # Set direction
  31. current_direction = f&quot;Clockwise&quot;
  32. # Set steps per feeding
  33. current_steps_feeding = 200
  34. # configure window
  35. self.title(&quot;Motor Options&quot;)
  36. self.geometry(f&quot;{750}x{385}&quot;)
  37. # configure grid layout (4x4)
  38. self.grid_columnconfigure((0, 1, 2, 3), weight=1)
  39. # create sidebar frame with widgets
  40. self.frame = customtkinter.CTkFrame(self, width=750, corner_radius=0)
  41. self.frame.grid(row=0, column=c, rowspan=3, sticky=&quot;nsew&quot;)
  42. # self.frame.grid_rowconfigure(9, weight=1)
  43. self.logo_label = customtkinter.CTkLabel(self.frame, text=&quot;Motor Options&quot;,
  44. font=customtkinter.CTkFont(size=20, weight=&quot;bold&quot;))
  45. self.logo_label.grid(row=r, column=c, columnspan=4, padx=20, pady=(10, 10))
  46. r += 1
  47. self.logo_label = customtkinter.CTkLabel(self.frame, text=&quot;Speed&quot;,
  48. font=customtkinter.CTkFont(size=20, weight=&quot;bold&quot;))
  49. self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))
  50. # Speed up
  51. c += 1
  52. self.button_1 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(1))
  53. self.button_1.grid(row=r, column=c, padx=20, pady=(10, 10))
  54. # Speed down
  55. c += 1
  56. self.button_2 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(2))
  57. self.button_2.grid(row=r, column=c, padx=20, pady=(10, 10))
  58. # create Speed entry box
  59. c += 1
  60. self.entry_speed = customtkinter.CTkEntry(self.frame, placeholder_text=speed)
  61. self.entry_speed.grid(row=r, column=c, padx=20, pady=(10, 10))
  62. # Direction
  63. c = 0
  64. r += 1
  65. self.logo_label = customtkinter.CTkLabel(self.frame, text=&quot;Direction&quot;,
  66. font=customtkinter.CTkFont(size=20, weight=&quot;bold&quot;))
  67. self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))
  68. # Clockwise
  69. c += 1
  70. self.button_3 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(3))
  71. self.button_3.grid(row=r, column=c, padx=20, pady=(10, 10))
  72. # Counter Clockwise
  73. c += 1
  74. self.button_4 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(4))
  75. self.button_4.grid(row=r, column=c, padx=20, pady=(10, 10))
  76. # create direction entry
  77. c += 1
  78. self.entry_direction = customtkinter.CTkEntry(self.frame, placeholder_text=direction)
  79. self.entry_direction.grid(row=r, column=c, padx=20, pady=(10, 10))
  80. # Steps per Feeding
  81. c = 0
  82. r += 1
  83. self.logo_label = customtkinter.CTkLabel(self.frame, text=&quot;Steps per Feeding&quot;,
  84. font=customtkinter.CTkFont(size=20, weight=&quot;bold&quot;))
  85. self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))
  86. # Steps per feed +100
  87. c += 1
  88. self.button_5 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(5))
  89. self.button_5.grid(row=r, column=c, padx=20, pady=(10, 10))
  90. # Steps per feed +10
  91. c += 1
  92. self.button_6 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(6))
  93. self.button_6.grid(row=r, column=c, padx=20, pady=(10, 10))
  94. # Steps per feed +1
  95. c += 1
  96. self.button_7 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(7))
  97. self.button_7.grid(row=r, column=c, padx=20, pady=(10, 10))
  98. # create steps per feeding box
  99. c = 0
  100. r += 1
  101. self.entry_speed = customtkinter.CTkEntry(self.frame, placeholder_text=feed_steps)
  102. self.entry_speed.grid(row=r, column=c, padx=20, pady=(10, 10))
  103. # Steps per feed -100
  104. c += 1
  105. self.button_8 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(8))
  106. self.button_8.grid(row=r, column=c, padx=20, pady=(10, 10))
  107. # Steps per feed -10
  108. c += 1
  109. self.button_9 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(9))
  110. self.button_9.grid(row=r, column=c, padx=20, pady=(10, 10))
  111. # Steps per feed -1
  112. c += 1
  113. self.button_10 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(10))
  114. self.button_10.grid(row=r, column=c, padx=20, pady=(10, 10))
  115. # Test
  116. c = 0
  117. r += 1
  118. self.logo_label = customtkinter.CTkLabel(self.frame, text=&quot;Test&quot;,
  119. font=customtkinter.CTkFont(size=20, weight=&quot;bold&quot;))
  120. self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))
  121. # Test feed
  122. c += 1
  123. self.button_11 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(11))
  124. self.button_11.grid(row=r, column=c, padx=20, pady=(10, 10))
  125. # Run motor
  126. c += 1
  127. self.button_12 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(12))
  128. self.button_12.grid(row=r, column=c, padx=20, pady=(10, 10))
  129. # Stop motor
  130. c += 1
  131. self.button_13 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(13))
  132. self.button_13.grid(row=r, column=c, padx=20, pady=(10, 10))
  133. # Save/Reset/Cancel
  134. c = 0
  135. r += 1
  136. self.logo_label = customtkinter.CTkLabel(self.frame, text=&quot;&quot;,
  137. font=customtkinter.CTkFont(size=20, weight=&quot;bold&quot;))
  138. self.logo_label.grid(row=r, column=c, padx=20, pady=(10, 10))
  139. # Save changes
  140. r += 1
  141. c += 1
  142. self.button_14 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(14))
  143. self.button_14.grid(row=r, column=c, padx=20, pady=(10, 10))
  144. # Reset
  145. c += 1
  146. self.button_15 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(15))
  147. self.button_15.grid(row=r, column=c, padx=20, pady=(10, 10))
  148. # Cancel
  149. c += 1
  150. self.button_16 = customtkinter.CTkButton(self.frame, command=lambda: self.button_event(16))
  151. self.button_16.grid(row=r, column=c, padx=20, pady=(10, 10))
  152. self.button_1.configure(text=&quot;Faster&quot;)
  153. self.button_2.configure(text=&quot;Slower&quot;)
  154. self.button_3.configure(text=&quot;CW&quot;)
  155. self.button_4.configure(text=&quot;CCW&quot;)
  156. self.button_5.configure(text=&quot;+100&quot;)
  157. self.button_6.configure(text=&quot;+10&quot;)
  158. self.button_7.configure(text=&quot;+1&quot;)
  159. self.button_8.configure(text=&quot;-100&quot;)
  160. self.button_9.configure(text=&quot;-10&quot;)
  161. self.button_10.configure(text=&quot;-1&quot;)
  162. self.button_11.configure(text=&quot;Feed Test&quot;)
  163. self.button_12.configure(text=&quot;Run Motor&quot;)
  164. self.button_13.configure(text=&quot;Stop Motor&quot;)
  165. self.button_14.configure(text=&quot;Save&quot;)
  166. self.button_15.configure(text=&quot;Reset&quot;)
  167. self.button_16.configure(text=&quot;Cancel&quot;)
  168. def button_event(self, buttonNum):
  169. global speed
  170. if buttonNum == 1:
  171. print(f&quot;Faster&quot;)
  172. if 0 &lt;= speed &lt; 100:
  173. speed += 1
  174. print(speed)
  175. self.entry_speed = customtkinter.CTkEntry(self.frame, placeholder_text=speed)
  176. elif buttonNum == 2:
  177. print(f&quot;Slower&quot;)
  178. if speed &gt; 0 and speed &lt; 101:
  179. speed -= 1
  180. print(speed)
  181. elif buttonNum == 3:
  182. print(f&quot;CW&quot;)
  183. elif buttonNum == 4:
  184. print(f&quot;CCW&quot;)
  185. elif buttonNum == 5:
  186. print(f&quot;+100&quot;)
  187. elif buttonNum == 6:
  188. print(f&quot;+10&quot;)
  189. elif buttonNum == 7:
  190. print(f&quot;+1&quot;)
  191. elif buttonNum == 8:
  192. print(f&quot;-100&quot;)
  193. elif buttonNum == 9:
  194. print(f&quot;-10&quot;)
  195. elif buttonNum == 10:
  196. print(f&quot;-1&quot;)
  197. elif buttonNum == 11:
  198. print(f&quot;Feed Test&quot;)
  199. elif buttonNum == 12:
  200. print(f&quot;Run Motor&quot;)
  201. elif buttonNum == 13:
  202. print(f&quot;Stop Motor&quot;)
  203. elif buttonNum == 14:
  204. print(f&quot;Save&quot;)
  205. elif buttonNum == 15:
  206. print(f&quot;Reset&quot;)
  207. elif buttonNum == 16:
  208. print(f&quot;Cancel&quot;)
  209. sys.exit(0)
  210. if __name__ == &quot;__main__&quot;:
  211. app = App()
  212. app.mainloop()

And here's the config.ini file.

  1. speed: 10
  2. direction = CW
  3. feed_steps: 150

答案1

得分: 1

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

  • self.entry_speed 变量被用于两个 CTkEntry 实例。请使用不同的变量来代替。
  • button_event() 函数中创建了一个新的 CTkEntry 实例。请使用 .configure() 方法来更新现有的小部件。
  1. class App(customtkinter.CTk):
  2. def __init__(self):
  3. ...
  4. # 创建每次喂食的步数输入框
  5. c = 0
  6. r += 1
  7. # 使用另一个实例变量来表示这个输入框
  8. self.entry_feeding = customtkinter.CTkEntry(self.frame, placeholder_text=feed_steps)
  9. self.entry_feeding.grid(row=r, column=c, padx=20, pady=(10, 10))
  10. ...
  11. def button_event(self, buttonNum):
  12. global speed
  13. if buttonNum == 1:
  14. print(f"更快")
  15. if 0 <= speed < 100:
  16. speed += 1
  17. print(speed)
  18. # 使用 .configure() 方法来更新小部件的选项
  19. self.entry_speed.configure(placeholder_text=speed)
  20. elif buttonNum == 2:
  21. print(f"更慢")
  22. if speed > 0 and speed < 101:
  23. speed -= 1
  24. print(speed)
  25. ...
英文:

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.
  1. class App(customtkinter.CTk):
  2. def __init__(self):
  3. ...
  4. # create steps per feeding box
  5. c = 0
  6. r += 1
  7. # use another instance variable for this entry
  8. self.entry_feeding = customtkinter.CTkEntry(self.frame, placeholder_text=feed_steps)
  9. self.entry_feeding.grid(row=r, column=c, padx=20, pady=(10, 10))
  10. ...
  11. def button_event(self, buttonNum):
  12. global speed
  13. if buttonNum == 1:
  14. print(f&quot;Faster&quot;)
  15. if 0 &lt;= speed &lt; 100:
  16. speed += 1
  17. print(speed)
  18. # use .configure() to update widget option
  19. self.entry_speed.configure(placeholder_text=speed)
  20. elif buttonNum == 2:
  21. print(f&quot;Slower&quot;)
  22. if speed &gt; 0 and speed &lt; 101:
  23. speed -= 1
  24. print(speed)
  25. ...

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:

确定