2017-12-19 11 views
0

백분율을 보여주는 진행률 표시 줄 중간에 레이블을 넣을 수 있습니까? 문제는 파이썬이 레이블 배경에 투명도를 지원하지 않기 때문에 어떻게 해결할 수 있는지 모르겠습니다.백분율 레이블이있는 진행 표시 줄?

+2

어쩌면 도움 [? 라벨의 배경은 Tkinter에서 투명하게 만드는 방법] (https://stackoverflow.com/questions/30180138/how- : 여기

코드입니다 Tkinter를 사용하여 투명한 위젯을 만드는 방법] (https://stackoverflow.com/questions/17039481/how-to-create-transparent)을 참조하십시오. -widgets-using-tkinter) – davedwards

답변

2

이것은 ttk.Style을 사용하여 가능합니다. 아이디어는 바 내부에 레이블을 추가 할 Horizontal.TProgressbar 스타일 (수직 ProgressBar의 Vertical.TProgressbar와 동일한 작업을 수행)의 레이아웃을 수정하는 것입니다 :

평소 Horizontal.TProgressbar 레이아웃 : 레이블이 추가로

[('Horizontal.Progressbar.trough', 
    {'children': [('Horizontal.Progressbar.pbar', 
    {'side': 'left', 'sticky': 'ns'})], 
    'sticky': 'nswe'})] 

:

[('Horizontal.Progressbar.trough', 
    {'children': [('Horizontal.Progressbar.pbar', 
    {'side': 'left', 'sticky': 'ns'})], 
    'sticky': 'nswe'}), 
('Horizontal.Progressbar.label', {'sticky': 'nswe'})] 

그런 다음 레이블의 텍스트는 style.configure으로 변경할 수 있습니다.

import tkinter as tk 
from tkinter import ttk 

root = tk.Tk() 

style = ttk.Style(root) 
# add label in the layout 
style.layout('text.Horizontal.TProgressbar', 
      [('Horizontal.Progressbar.trough', 
       {'children': [('Horizontal.Progressbar.pbar', 
           {'side': 'left', 'sticky': 'ns'})], 
       'sticky': 'nswe'}), 
       ('Horizontal.Progressbar.label', {'sticky': ''})]) 
# set initial text 
style.configure('text.Horizontal.TProgressbar', text='0 %') 
# create progressbar 
variable = tk.DoubleVar(root) 
pbar = ttk.Progressbar(root, style='text.Horizontal.TProgressbar', variable=variable) 
pbar.pack() 

def increment(): 
    pbar.step() # increment progressbar 
    style.configure('text.Horizontal.TProgressbar', 
        text='{:g} %'.format(variable.get())) # update label 
    root.after(200, increment) 

increment() 

root.mainloop() 

screenshot of the result

+0

아주 좋은 해결책입니다! 그러나 내 편'TypeError : getint() 인수는 23 행에서 부동이어야하며 str이어야합니다. 그러나'variable' 유형을'StringVar'로 변경하면 예상대로 작동합니다! – CommonSense

+0

@CommonSense 피드백에 감사드립니다, 내 컴퓨터에 오류가 발생하지 않습니다, 당신의 경우에는 플로트 것으로 보인다 progressbar의 현재 값의 형식에 따라 달라집니다 같아요. 나는 대답을 고쳐 줄 것이다. –

+0

@CommonSense'DoubleVar'와 함께 작동합니까? –