Home > database >  Disk Clean up in GUI tkinter using Python Script
Disk Clean up in GUI tkinter using Python Script

Time:10-03

I am new to python and I trying input the disk clean up function into the button, when I click the button then it will only start run the function. But I met problem that when I start run the program, the disk clean up function will run first and later only show my GUI, then I click the button is no function at all. Please help me to make the script work.

class Page3(Page):
   def __init__(self, *args, **kwargs):
      Page.__init__(self, *args, **kwargs,bg='white')
      label = tkk.Label(self, text="This is page 3")
      label.grid(row=2,column=1)
      def mytools():
           total, used, free = shutil.disk_usage("/")
           print("Total:%d GB" %(total // (2**30)))
           print("Used:%d GB" %(used // (2**30)))
           print("Free:%d GB" %(free // (2**30)))

           if free <= total/2:
               clean = os.popen('Cleanmgr.exe/ sagerun:1').read()
               print(clean)
      def btn1():
           if __name__ =="__main__":
              mytools()
      button1=ttk.Button(self,text="Clean Up",command=btn1())
      button1.grid(row=3,column=2)

CodePudding user response:

command=btn1() the brackets () is the syntax to call the function. Therefore by initiating/creating your Button widget, your function gets called. You just need a reference to that function as an argument, it gets called event driven by clicking on that button.

To solve your issue edit this line like this: button1=ttk.Button(self,text="Clean Up",command=btn1)

Hint: It makes sense to use the syntax self. for your widgets, you may once want to address these widgets after initiating the class.

Hint 2: If you also once want to use these functions after initiating the class. You will need to fix the indentation and add the positional argument self to it.

  • Related