|
| 1 | +rom Tkinter import * |
| 2 | +import ttk |
| 3 | + |
| 4 | +class MainWindow(Frame): |
| 5 | + |
| 6 | + def __init__(self): |
| 7 | + Frame.__init__(self) |
| 8 | + self.master.title("ProgressBar example") |
| 9 | + #self.master.minsize(400, 100) |
| 10 | + self.grid(sticky=E+W+N+S) |
| 11 | + |
| 12 | + #----- |
| 13 | + |
| 14 | + self.var_ind = IntVar(self) |
| 15 | + |
| 16 | + self.pbar_ind = ttk.Progressbar(self, orient="horizontal", length=400, mode="indeterminate", variable=self.var_ind, maximum=100) |
| 17 | + self.pbar_ind.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S, columnspan=3) |
| 18 | + |
| 19 | + Label(self, text="indeterminate").grid(row=1, column=0, pady=2, padx=2, sticky=E+W+N+S) |
| 20 | + |
| 21 | + self.lab_ind_var = Label(self, text="VAR:") |
| 22 | + self.lab_ind_var.grid(row=1, column=1, pady=2, padx=2, sticky=E+W+N+S) |
| 23 | + |
| 24 | + self.lab_ind_max = Label(self, text="MAX:") |
| 25 | + self.lab_ind_max.grid(row=1, column=2, pady=2, padx=2, sticky=E+W+N+S) |
| 26 | + |
| 27 | + #----- |
| 28 | + |
| 29 | + self.var_det = IntVar(self) |
| 30 | + |
| 31 | + self.pbar_det = ttk.Progressbar(self, orient="horizontal", length=400, mode="determinate", variable=self.var_det, maximum=100) |
| 32 | + self.pbar_det.grid(row=2, column=0, pady=2, padx=2, sticky=E+W+N+S, columnspan=3) |
| 33 | + |
| 34 | + Label(self, text="determinate").grid(row=3, column=0, pady=2, padx=2, sticky=E+W+N+S) |
| 35 | + |
| 36 | + self.lab_det_var = Label(self, text="VAR:") |
| 37 | + self.lab_det_var.grid(row=3, column=1, pady=2, padx=2, sticky=E+W+N+S) |
| 38 | + |
| 39 | + self.lab_det_max = Label(self, text="MAX:") |
| 40 | + self.lab_det_max.grid(row=3, column=2, pady=2, padx=2, sticky=E+W+N+S) |
| 41 | + |
| 42 | + #----- |
| 43 | + |
| 44 | + Label(self, text="ANIMATION:").grid(row=4, column=0, pady=2, padx=2, sticky=E+W+N+S) |
| 45 | + |
| 46 | + Button(self, text='START', command=self.animation_start).grid(row=4, column=1, pady=2, padx=2, sticky=E+W+N+S) |
| 47 | + Button(self, text='STOP', command=self.animation_stop).grid(row=4, column=2, pady=2, padx=2, sticky=E+W+N+S) |
| 48 | + |
| 49 | + #----- |
| 50 | + |
| 51 | + Label(self, text="SET:").grid(row=5, column=0, pady=2, padx=2, sticky=E+W+N+S) |
| 52 | + |
| 53 | + Button(self, text='BEGIN', command=self.set_begin).grid(row=5, column=1, pady=2, padx=2, sticky=E+W+N+S) |
| 54 | + Button(self, text='END', command=self.set_end).grid(row=5, column=2, pady=2, padx=2, sticky=E+W+N+S) |
| 55 | + |
| 56 | + #----- |
| 57 | + |
| 58 | + Label(self, text="SET:").grid(row=6, column=0, pady=2, padx=2, sticky=E+W+N+S) |
| 59 | + |
| 60 | + Button(self, text='23', command=lambda:self.set(23)).grid(row=6, column=1, pady=2, padx=2, sticky=E+W+N+S) |
| 61 | + Button(self, text='77', command=lambda:self.set(77)).grid(row=6, column=2, pady=2, padx=2, sticky=E+W+N+S) |
| 62 | + |
| 63 | + #----- |
| 64 | + |
| 65 | + Label(self, text="SET:").grid(row=7, column=0, pady=2, padx=2, sticky=E+W+N+S) |
| 66 | + |
| 67 | + Button(self, text='+23', command=lambda:self.set_plus(23)).grid(row=7, column=1, pady=2, padx=2, sticky=E+W+N+S) |
| 68 | + Button(self, text='-77', command=lambda:self.set_plus(-77)).grid(row=7, column=2, pady=2, padx=2, sticky=E+W+N+S) |
| 69 | + |
| 70 | + #----- |
| 71 | + |
| 72 | + Label(self, text="STEP:").grid(row=8, column=0, pady=2, padx=2, sticky=E+W+N+S) |
| 73 | + |
| 74 | + Button(self, text='+1', command=lambda:self.step(1)).grid(row=8, column=1, pady=2, padx=2, sticky=E+W+N+S) |
| 75 | + Button(self, text='-10', command=lambda:self.step(-10)).grid(row=8, column=2, pady=2, padx=2, sticky=E+W+N+S) |
| 76 | + |
| 77 | + #----- |
| 78 | + |
| 79 | + Label(self, text="MAX:").grid(row=9, column=0, pady=2, padx=2, sticky=E+W+N+S) |
| 80 | + |
| 81 | + Button(self, text='100', command=lambda:self.max(100)).grid(row=9, column=1, pady=2, padx=2, sticky=E+W+N+S) |
| 82 | + Button(self, text='200', command=lambda:self.max(200)).grid(row=9, column=2, pady=2, padx=2, sticky=E+W+N+S) |
| 83 | + |
| 84 | + #----- |
| 85 | + |
| 86 | + self.update_labels_after = False |
| 87 | + self.update_labels() |
| 88 | + |
| 89 | + #----- |
| 90 | + |
| 91 | + def animation_start(self): |
| 92 | + self.update_labels_after = True |
| 93 | + |
| 94 | + self.pbar_ind.start() # .start(1) |
| 95 | + self.pbar_det.start() # .start(1) |
| 96 | + |
| 97 | + self.update_labels() |
| 98 | + |
| 99 | + def animation_stop(self): |
| 100 | + self.update_labels_after = False |
| 101 | + |
| 102 | + self.pbar_ind.stop() |
| 103 | + self.pbar_det.stop() |
| 104 | + |
| 105 | + self.update_labels() |
| 106 | + |
| 107 | + #----- |
| 108 | + |
| 109 | + def set_begin(self): |
| 110 | + self.var_ind.set( 0 ) |
| 111 | + self.var_det.set( 0 ) |
| 112 | + |
| 113 | + self.update_labels() |
| 114 | + |
| 115 | + def set_end(self): |
| 116 | + self.var_ind.set( self.pbar_ind.cget('maximum') ) |
| 117 | + self.var_det.set( self.pbar_det.cget('maximum') ) |
| 118 | + |
| 119 | + self.update_labels() |
| 120 | + |
| 121 | + #----- |
| 122 | + |
| 123 | + def set(self, val): |
| 124 | + self.var_ind.set( val ) |
| 125 | + self.var_det.set( val ) |
| 126 | + |
| 127 | + self.update_labels() |
| 128 | + |
| 129 | + #----- |
| 130 | + |
| 131 | + def set_plus(self, val): |
| 132 | + self.var_ind.set( self.var_ind.get() + val ) |
| 133 | + self.var_det.set( self.var_det.get() + val ) |
| 134 | + |
| 135 | + self.update_labels() |
| 136 | + |
| 137 | + #----- |
| 138 | + |
| 139 | + def step(self, val=1): |
| 140 | + self.pbar_ind.step(val) # .step() |
| 141 | + self.pbar_det.step(val) # .step() |
| 142 | + |
| 143 | + self.update_labels() |
| 144 | + |
| 145 | + #----- |
| 146 | + |
| 147 | + def max(self, val=1): |
| 148 | + self.pbar_ind.config(maximum=val) |
| 149 | + self.pbar_det.config(maximum=val) |
| 150 | + |
| 151 | + self.update_labels() |
| 152 | + |
| 153 | + #----- |
| 154 | + |
| 155 | + def update_labels(self): |
| 156 | + |
| 157 | + self.lab_ind_var.config(text='VAR: %d' % (self.var_ind.get())) |
| 158 | + self.lab_ind_max.config(text='MAX: %d' % (self.pbar_ind.cget('maximum'))) |
| 159 | + |
| 160 | + self.lab_det_var.config(text='VAR: %d' % (self.var_det.get())) |
| 161 | + self.lab_det_max.config(text='MAX: %d' % (self.pbar_det.cget('maximum'))) |
| 162 | + |
| 163 | + if self.update_labels_after: |
| 164 | + self.after(50, self.update_labels) |
| 165 | + |
| 166 | +if __name__=="__main__": |
| 167 | + MainWindow().mainloop() |
0 commit comments