Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Label doesn't refresh
#1
With below code, the label of directory path doesn't be refreshed correctly after I change the directory with "Ctrl+o".

E.g. When you change it to "C:\", you can see the new label is displayed on old label. How to refresh it to show the new label only?

I searched on the internet for 2 dys, no matter update(), destroy() or anything working in my Python v3.6.5.


import tkinter
import tkinter.filedialog
import os


class fileData:

     def __init__(self):
          self.targetDir = "./"
          self.mainWin = tkinter.Tk()

     def reportShow(self):          

          ln1 = 2
          tkinter.Label(self.mainWin, text = "     Target   ",justify = tkinter.LEFT).grid(row = ln1, column =0)
          absPath = os.path.abspath(self.targetDir)
          if len(absPath) > 40:  absPath = ' ...%s' % absPath[-37:]     
          tkinter.Label(self.mainWin, text = absPath, background='white').grid(row = ln1, column = 1, columnspan = 3)                    


def chooseFolder():                                                   #This is where we lauch the directory change bar
     filesData.targetDir = tkinter.filedialog.askdirectory(title = "Choose the target directory", initialdir=filesData.targetDir)
     filesData.reportShow()
     
 
     
def showMain():
     mainWin = filesData.mainWin
     
     winSize = 360     
     mainWin.geometry('%dx%d+%d+%d' % (winSize, winSize, (mainWin.winfo_screenwidth()-winSize)/2,
                    (mainWin.winfo_screenheight()-winSize)/2))        #The size and position of the main window
     mainWin.minsize(winSize,winSize)
     mainWin.bind('<Control-o>',lambda event: chooseFolder())

     filesData.reportShow()
     mainWin.mainloop()


filesData = fileData()


if __name__=="__main__":
    showMain()

Quote
#2
it works by commenting the line

#          if len(absPath) > 40:  absPath = ' ...%s' % absPath[-37:]  
There must be a problem with this line
Quote
#3
add to __init__
    self.abspathv = tkinter.StringVar()
Then modify Label:
tkinter.Label(self.mainWin, text = absPath, textvariable=self.abspathv, background='white').grid(row = ln1, column = 1, columnspan = 3)
and finally set it with:
self.abspath.set('vlaue goes here')
Quote
#4
(Jul-10-2018, 10:41 AM)Axel_Erfurt Wrote: it works by commenting the line

#          if len(absPath) > 40:  absPath = ' ...%s' % absPath[-37:]  
There must be a problem with this line

Thanks for reminding. But why is it that line? How to modify?

(Jul-10-2018, 02:01 PM)Larz60+ Wrote: add to __init__

Thank you so much and it works. But why the variable "tkinter.StringVar()" must be defined in "__init__()"? Why can't in the function of class?
Quote
#5
It doesn't have to be in __init__.
This is a habit of mine as I tend to access from several methods, but it can be used in local scope.
Quote
#6
(Jul-11-2018, 07:55 AM)Larz60+ Wrote: It doesn't have to be in __init__.
This is a habit of mine as I tend to access from several methods, but it can be used in local scope.

No, it doesn't work in local scope.
I moved it to local scope as the code below and you can try.
You'll find the label doesn't refresh correctly if changing the directory path from a long one to short.

import tkinter
import tkinter.filedialog
import os
 
 
class fileData:
 
     def __init__(self):
          self.targetDir = "./"
          self.mainWin = tkinter.Tk()
 
     def reportShow(self):          
 
          ln1 = 2
          absPathv = tkinter.StringVar()
          tkinter.Label(self.mainWin, text = "     Target   ",justify = tkinter.LEFT).grid(row = ln1, column =0)
          absPath = os.path.abspath(self.targetDir)
          if len(absPath) > 40:  absPath = ' ...%s' % absPath[-37:]
          absPathv.set(absPath)
          tkinter.Label(self.mainWin, textvariable = absPathv, background='white').grid(row = ln1, column = 1, columnspan = 3)                    
 
 
def chooseFolder():                                                   #This is where we lauch the directory change bar
     filesData.targetDir = tkinter.filedialog.askdirectory(title = "Choose the target directory", initialdir=filesData.targetDir)
     filesData.reportShow()
      
  
      
def showMain():
     mainWin = filesData.mainWin
      
     winSize = 360     
     mainWin.geometry('%dx%d+%d+%d' % (winSize, winSize, (mainWin.winfo_screenwidth()-winSize)/2,
                    (mainWin.winfo_screenheight()-winSize)/2))        #The size and position of the main window
     mainWin.minsize(winSize,winSize)
     mainWin.bind('<Control-o>',lambda event: chooseFolder())
 
     filesData.reportShow()
     mainWin.mainloop()
 
 
filesData = fileData()
 
 
if __name__=="__main__":
    showMain()

Quote
#7
And if it works in __init__ is what is the objection to keeping it there? It's still local to the class.
Quote
#8
(Jul-12-2018, 12:21 PM)Larz60+ Wrote: And if it works in __init__ is what is the objection to keeping it there? It's still local to the class.

Got it and thank you very much.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to change the text in label? ;/ Dzonzi 3 37 6 hours ago
Last Post: nilamo
  [PyQt] Assign Label Text as number in Hz mekha 17 483 Yesterday, 12:47 AM
Last Post: mekha
  Update Gtk.Label in for-loop TimeMen 3 155 Jun-07-2018, 10:33 PM
Last Post: killerrex
  [PyQt] enviremont refresh duende 2 151 May-13-2018, 09:49 AM
Last Post: duende
  pyqt main window refresh poblems duende 0 268 Apr-13-2018, 05:05 PM
Last Post: duende
  [Kivy] AttributeError setting the label text gio123 3 293 Mar-15-2018, 09:30 AM
Last Post: buran
  Refresh test in urwid.Text Stensborg 1 223 Mar-05-2018, 11:23 AM
Last Post: Stensborg
  [Tkinter] problem with refresh UI in tkinter app Philbot 5 694 Feb-06-2018, 01:10 PM
Last Post: Philbot
  How Can I Do Refresh Window? satrancali 0 426 Feb-03-2018, 07:50 AM
Last Post: satrancali
  [Kivy] Installed but doesn't work antonmichel 13 917 Jan-30-2018, 10:34 AM
Last Post: antonmichel

Forum Jump:


Users browsing this thread: 1 Guest(s)