当我将滑块向下移动到更低的值(例如,到40)时,标签中的第三个数字仍然显示?

我怎么才能解决这个问题呢?

import tkinter as tk

class App(tk.Tk):

        
  def __init__(self):
    super().__init__()
    
    # BMP display
    def bmp_print(*args):
        self.bmp_number = tk.Label(self, text=[self.w1.get()], fg = 'red', font=('Times', 60))
        self.bmp_number.place(x = 725, y = 140)
        
    
    # self windows size
    window_width = 1024
    window_height = 600

    # get the screen dimension
    screen_width = self.winfo_screenwidth()
    screen_height = self.winfo_screenheight()

    # find the center point
    center_x = int(screen_width/2 - window_width / 2)
    center_y = int(screen_height/2 - window_height / 2)

    # set the position of the window to the center of the screen
    self.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
    
    # Resize main window xy, on/off,  0 or 1 .
    self.resizable(0, 0)
    
    # Main window on top of stack.
    self.attributes('-topmost', 1) 
    
    # Create slider
    self.w1 = tk.Scale(self, from_=240, to=40, command = bmp_print)
    self.w1.set(240)
    self.w1.place(x = 150, y=400)

if __name__ == "__main__":
  app = App()
  app.mainloop()


预计当结果从三位数值变为两位数值时,标签将显示两位数值,而不显示剩余的第三位数值.

推荐答案

当我将滑块向下移动到更低的值(例如,到40)时, 标签中的第三个数字是否仍显示?

self.bmp_number移出函数,并在bmp_print(args)函数内添加self.bmp_number.configure.

代码片段:

import tkinter as tk

class App(tk.Tk):

        
  def __init__(self):
    super().__init__()

    
    # BMP display
    def bmp_print(args):
        #self.bmp_number = tk.Label(self, text=self.w1.get(),from_ = 0, to =10, fg = 'red', font=('Times', 60))
        #self.bmp_number.place(x = 725, y = 140)
        self.bmp_number.configure(text=args)
        
    
    # self windows size
    window_width = 1024
    window_height = 600

    # get the screen dimension
    screen_width = self.winfo_screenwidth()
    screen_height = self.winfo_screenheight()

    # find the center point
    center_x = int(screen_width/2 - window_width / 2)
    center_y = int(screen_height/2 - window_height / 2)

    # set the position of the window to the center of the screen
    self.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
    
    # Resize main window xy, on/off,  0 or 1 .
    self.resizable(0, 0)
    
    # Main window on top of stack.
    self.attributes('-topmost', 1)

     
    
    # Create slider
    self.w1 = tk.Scale(self, from_=240, to=40, command = bmp_print)
    self.w1.set(240)
    self.w1.place(x = 150, y=400)

    self.bmp_number = tk.Label(self, text=self.w1.get(), fg = 'red', font=('Times', 60))
    self.bmp_number.place(x = 725, y = 140)

if __name__ == "__main__":
  app = App()
  app.mainloop()

屏幕截图:

enter image description here

Python相关问答推荐

尽管进程输出错误消息,subProcess.check_call的CalledProcess错误.stderr为无

从单个列创建多个列并按pandas分组

使用Beautiful Soup获取第二个srcset属性

从今天起的future 12个月内使用Python迭代

如何在Python中使用io.BytesIO写入现有缓冲区?

Python daskValue错误:无法识别的区块管理器dask -必须是以下之一:[]

试图找到Python方法来部分填充numpy数组

Python中的嵌套Ruby哈希

' osmnx.shortest_track '返回有效源 node 和目标 node 的'无'

Python库:可选地支持numpy类型,而不依赖于numpy

如何获取TFIDF Transformer中的值?

修复mypy错误-赋值中的类型不兼容(表达式具有类型xxx,变量具有类型yyy)

如何使用pytest来查看Python中是否存在class attribution属性?

如何在Raspberry Pi上检测USB并使用Python访问它?

优化器的运行顺序影响PyTorch中的预测

如何保持服务器发送的事件连接活动?

旋转多边形而不改变内部空间关系

lityter不让我输入左边的方括号,'

为什么if2/if3会提供两种不同的输出?

基于行条件计算(pandas)