【问题标题】:AttributeError: 'users_Arduino' object has no attribute 'Progressbar'AttributeError:“users_Arduino”对象没有属性“Progressbar”
【发布时间】:2023-03-05 11:04:01
【问题描述】:

我尝试了几种方法,用 ttk.Progressbar 设置它,用 self.progresso.Progressbar 设置它......什么都没有,有人可以帮忙吗?我还年轻,对不起我的无知。如果他们能尽可能清楚,谢谢。

from tkinter import *
from tkinter import ttk
import time

class users_Arduino:

    def __init__(self,window):
        self.wind = window
        self.wind.title("System F2T - Cadastro Arduino")

        menubar = Menu(window)
        arduino = Menu(menubar,tearoff=0)
        menubar.add_cascade(label = "Arduino",menu=arduino)
        arduino.add_command(label = "Conectar/Inserir dados-BD", command=self.getSerialData)
        window.config(menu = menubar)

    def bar(): 
        progress['value'] = 20
        root.update_idletasks() 
        time.sleep(1) 

        progress['value'] = 40
        root.update_idletasks() 
        time.sleep(1) 

        progress['value'] = 50
        root.update_idletasks() 
        time.sleep(1) 

        progress['value'] = 60
        root.update_idletasks() 
        time.sleep(1) 

        progress['value'] = 80
        root.update_idletasks() 
        time.sleep(1) 
        progress['value'] = 100   

    def getSerialData(self):
        self.progresso = Toplevel()
        self.progresso.title("System F2T - Progress")
        self.progresso.geometry("290x200")
        #self.progresso["bg"] = "#000"
        progress = self.Progressbar(self.progresso,orient = HORIZONTAL, length = 100, mode = 'determinate').pack(pady = 10) 
        Button(self.progresso, text = 'Start', command = self.bar).pack(pady = 10) 

if __name__ == '__main__':
    window = Tk()
    window['bg'] = "#000"
    users_Arduino(window)
    window.mainloop()

【问题讨论】:

    标签: python-3.x tkinter progress-bar


    【解决方案1】:

    您正在尝试使用self.Progressbar 访问类属性,这显然行不通。您的意图是创建一个Progressbar,应按如下方式完成:

    progress = ttk.Progressbar(self.progresso,orient = HORIZONTAL, length = 100, mode = 'determinate').pack(pady = 10)
    

    接下来,您希望进度条每秒更新一次,直到达到 100,但调用 time.sleep 会阻塞您的主线程并冻结您的 GUI。您需要使用root.after 方法。

    此外,如果您在同一行调用 something=widget(...).pack(),则您没有保持对该对象的正确引用。返回值将只是None

    一切都结束了:

    from tkinter import *
    from tkinter import ttk
    
    class UsersArduino:
    
        def __init__(self,window):
            self.wind = window
            self.wind.title("System F2T - Cadastro Arduino")
            self.value = 0
            menubar = Menu(window)
            arduino = Menu(menubar,tearoff=0)
            menubar.add_cascade(label = "Arduino",menu=arduino)
            arduino.add_command(label = "Conectar/Inserir dados-BD", command=self.getSerialData)
            window.config(menu = menubar)
    
        def bar(self):
            self.progress['value'] +=20
            if self.progress['value'] <=100:
                self.wind.after(1000,self.bar)
    
        def getSerialData(self):
            self.progresso = Toplevel()
            self.progresso.title("System F2T - Progress")
            self.progresso.geometry("290x200")
            self.progress = ttk.Progressbar(self.progresso,orient = HORIZONTAL, length = 100, mode = 'determinate')
            self.progress.pack(pady = 10)
            Button(self.progresso, text = 'Start', command = self.bar).pack(pady = 10)
    
    if __name__ == '__main__':
        window = Tk()
        window['bg'] = "#000"
        UsersArduino(window)
        window.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2012-12-01
      • 2021-04-19
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多