Python Forum
[PyGUI] Python 3.8.1 Tkinter Widget stete change
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGUI] Python 3.8.1 Tkinter Widget stete change
#1
Dear Sirs,
This is my python code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from tkinter import *
 
import math
import numpy as np
 
gui = Tk()
gui.title("Money Split")
gui.geometry("580x200")
 
v1 = IntVar()
v2 = IntVar()
v3 = IntVar()
v4 = IntVar()
 
def testVal(inStr,i,acttyp):
    ind=int(i)
    if acttyp == '1': #insert
        if not inStr[ind].isdigit():
            return False
    return True
 
R = 4
C = 3
 
valore = StringVar()
 
# Initialize np
np = [[50, 0, 0],
          [20, 0, 0],
      [10, 0, 0],
      [5, 0, 0]]
   
# For printing the np
def shownp():
    for i in range(R):
        for j in range(C):
            print(np[i][j], end = " ")
        print()
 
#create a callback for our button
def callback():
  global b50
  global a1
   
  if e0.get() != '':
    importo = int(e0.get())
    status_label['text'] = importo      
    if importo % 5 == 0 and importo >= 5:
      if v1.get():
        b50 = int(e1.get())
      else:
        if importo >= 50:
            b50 = math.ceil(importo / 85)
        else:
            b50=0
      np [0][2] = b50
      a1 = str(b50)
      e1.delete(0, END)
      e1.insert(0, a1)
         
      if v2.get():
        b20 = int(e2.get())
      else:
        if (importo - b50 * 50) >= 20:
            b20 = math.ceil((importo - b50 * 50) / 35)
        else:
            b20=0
      np [1][2] = b20
      a2 = str(b20)
      e2.delete(0, END)
      e2.insert(0, a2)
         
      if v3.get():
        b10 = int(e3.get())
      else:
        if (importo - b50 * 50 - b20 * 20) >= 10:
            b10 = math.ceil((importo - b50 * 50 - b20 * 20) / 15)
        else:
            b10=0
      np [2][2] = b10
      a3 = str(b10)
      e3.delete(0, END)
      e3.insert(0, a3)
         
      if v4.get():
        b5 = int(e4.get())
      else:
        if (importo - b50 * 50 - b20 * 20 - b10 * 10) >= 5:
            b5 = math.ceil((importo - b50 * 50 - b20 * 20 - b10 * 10) / 5)
        else:
            b5 = 0   
      np [3][2] = b5
      a4 = str(b5)
      e4.delete(0, END)
      e4.insert(0, a4)
 
      print (b50 * 50 + b20 * 20 + b10 * 10 + b5 * 5)
      shownp()
      status_label['text'] = 'Done!'
      b1.state('normal')
      b2['state'] = 'normal'
      b3['state'] = 'normal'
      b4['state'] = 'normal'
 
    else:
      status_label['text'] = 'Insert a number divisible by 5'
      e0.delete(0,END)
      e1.delete(0,END)
      e2.delete(0,END)
      e3.delete(0,END)
      e4.delete(0,END)
      b1.configure(state='disabled')
      b2.configure(state='disabled')
      b3.configure(state='disabled')
      b4.configure(state='disabled')
      e0.focus
 
def change50(v1):
    print(v1)
    e1.delete(0,END)
    e2.delete(0,END)
    e3.delete(0,END)
    e4.delete(0,END)
    e1.insert(0, np [0][2] + v1)
    np [0][1] = 1
    callback()
 
def change20(v2):
    e1.delete(0,END)
    e2.delete(0,END)
    e3.delete(0,END)
    e4.delete(0,END)
    e2.insert(0, np [1][2] + v2)
    np [1][1] = 1
    callback()
 
def change10(v3):
    e1.delete(0,END)
    e2.delete(0,END)
    e3.delete(0,END)
    e4.delete(0,END)
    e3.insert(0, np [2][2] + v3)
    np [2][1] = 1
    callback()
 
def change5(v4):
    e1.delete(0,END)
    e2.delete(0,END)
    e3.delete(0,END)
    e4.delete(0,END)
    e3.insert(0, np [3][2] + v4)
    np [3][1] = 1
    callback()
 
def verifica(event=None):
    val1 = v1.get() * int(e1.get()) * 50
    val2 = v2.get() * int(e2.get()) * 20
    val3 = v3.get() * int(e3.get()) * 10
    val4 = v4.get() * int(e4.get()) * 5
    print (val1,val2,val3,val4)
    somma=val1+val2+val3+val4
    print (somma)
    valtot=int(e0.get())   
    if somma > valtot:
       b0.config(state = 'disabled')
       status_label['text'] = 'Change the fixed number of banknotes because it exceeds the amount to be split!'
    else:
       b0.config(state=NORMAL)
        
Label(gui, text="Importo").place(x=5, y=5, width=50)
Label(gui, text="50,00 €").place(x=5, y=35, width=50)
Label(gui, text="20,00 €").place(x=5, y=65, width=50)
Label(gui, text="10,00 €").place(x=5, y=95, width=50)
Label(gui, text="5,00 €").place(x=5, y=125, width=50)
 
a1 = StringVar()
a2 = StringVar()
a3 = StringVar()
a4 = StringVar()
 
e0 = Entry(gui, validate="key", justify='center')
e0['validatecommand'] = (e0.register(testVal),'%P','%i','%d')
 
e1 = Entry(gui, textvariable = a1, justify='center')
e2 = Entry(gui, textvariable = a2, justify='center')
e3 = Entry(gui, textvariable = a3, justify='center')
e4 = Entry(gui, textvariable = a4, justify='center')
 
#e0.grid(row=0, column=1)
e0.place(x=100, y=5, width=100)
e1.place(x=100, y=35, width=100)
e2.place(x=100, y=65, width=100)
e3.place(x=100, y=95, width=100)
e4.place(x=100, y=125, width=100)
 
b0 = Button(gui, text="Split", state='normal', command=callback).place(x=100, y=155, height=20, width=100)
b1 = Checkbutton(gui, text="fix", state='disabled', variable=v1, command=verifica).place(x=210, y=35, height=20, width=100)
b2 = Checkbutton(gui, text="fix", state='disabled',variable=v2, command=verifica).place(x=210, y=65, height=20, width=100)
b3 = Checkbutton(gui, text="fix", state='disabled',variable=v3, command=verifica).place(x=210, y=95, height=20, width=100)
b4 = Checkbutton(gui, text="fix", state='disabled',variable=v4, command=verifica).place(x=210, y=125, height=20, width=100)
b5 = Button(gui, text="Quit", command=gui.destroy).place(x=370, y=155, height=20, width=100)
 
status_label =Label(gui, height =3, width =35, bg ="black", fg ="#00FF00", text ="---", wraplength =200)
status_label.place(x=300, y=35, width=250)
 
e0.focus()
 
gui.mainloop()
I don't know why change button and checkbutton state is impossible for me.
I tried to use:

b1.state('normal')
b2['state'] = 'normal'
b0.config(state = 'disabled')
b0.config(state=NORMAL)


no one works! Angry
Why? Huh
Thanks for Your help
Best Regards
Reply
#2
step one, please change all cryptic letter names to meaningful values.
Very hard to read without.
your entry statements either need embedded 'command' of external bind statements.
Reply
#3
Thank You Larz60+!
I'm new in python.
You are right about the incomprehensibility of what I wrote.
I try to simplify:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from tkinter import *
 
import math
import numpy as np
 
# omissis...
 
def verifica(event=None):
    val1 = v1.get() * int(e1.get()) * 50
    val2 = v2.get() * int(e2.get()) * 20
    val3 = v3.get() * int(e3.get()) * 10
    val4 = v4.get() * int(e4.get()) * 5
    print (val1,val2,val3,val4)
    somma=val1+val2+val3+val4
    print (somma)
    valtot=int(e0.get())   
    if somma > valtot:
       b0.config(state = 'disabled') #<--- this doesn't work!!!
    else:
       b0.config(state=NORMAL) #<--- this doesn't work!!!
 
 
b0 = Button(gui, text="Split", state='normal', command=callback).place(x=100, y=155, height=20, width=100)
b1 = Checkbutton(gui, text="fix", state='disabled', variable=v1, command=verifica).place(x=210, y=35, height=20, width=100)
The problem lies in the fact that once the widgets (in this case b0 and b1) are set, I can no longer change their state (normal / disabled).
Have a nice day!
Reply
#4
You need a reference to your Entries and CheckButtons.

CheckButton(...).place(...) return None.
To get the reference to the CheckButton, create first an instance of the object, assign it to a name and then you can use the method place, which return always None.
1
2
3
4
5
6
# instead of
b1 = CheckButton(...).place(...)
 
# do this
b1 = CheckButton(...)
b1.place(...)
Here your code, corrected imports, removed numpy, removed some globals, optimized imports and blacked.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import math
from tkinter import (
    Button,
    Checkbutton,
    IntVar,
    StringVar,
    Tk,
    Label,
    Entry,
    END,
    NORMAL,
)
 
gui = Tk()
gui.title("Money Split")
gui.geometry("580x200")
 
v1 = IntVar()
v2 = IntVar()
v3 = IntVar()
v4 = IntVar()
 
 
def test_val(in_str, i, act_type):
    ind = int(i)
    if act_type == "1"# insert
        if not in_str[ind].isdigit():
            return False
    return True
 
 
R = 4
C = 3
 
valore = StringVar()
 
# Initialize np
np = [[50, 0, 0], [20, 0, 0], [10, 0, 0], [5, 0, 0]]
 
 
# For printing the np
def shownp():
    for i in range(R):
        for j in range(C):
            print(np[i][j], end=" ")
        print()
 
 
# create a callback for our button
def callback():
    global a1
 
    if e0.get() != "":
        importo = int(e0.get())
        status_label["text"] = importo
        if importo % 5 == 0 and importo >= 5:
            if v1.get():
                b50 = int(e1.get())
            else:
                if importo >= 50:
                    b50 = math.ceil(importo / 85)
                else:
                    b50 = 0
            np[0][2] = b50
            a1 = str(b50)
            e1.delete(0, END)
            e1.insert(0, a1)
 
            if v2.get():
                b20 = int(e2.get())
            else:
                if (importo - b50 * 50) >= 20:
                    b20 = math.ceil((importo - b50 * 50) / 35)
                else:
                    b20 = 0
            np[1][2] = b20
            a2 = str(b20)
            e2.delete(0, END)
            e2.insert(0, a2)
 
            if v3.get():
                b10 = int(e3.get())
            else:
                if (importo - b50 * 50 - b20 * 20) >= 10:
                    b10 = math.ceil((importo - b50 * 50 - b20 * 20) / 15)
                else:
                    b10 = 0
            np[2][2] = b10
            a3 = str(b10)
            e3.delete(0, END)
            e3.insert(0, a3)
 
            if v4.get():
                b5 = int(e4.get())
            else:
                if (importo - b50 * 50 - b20 * 20 - b10 * 10) >= 5:
                    b5 = math.ceil((importo - b50 * 50 - b20 * 20 - b10 * 10) / 5)
                else:
                    b5 = 0
            np[3][2] = b5
            a4 = str(b5)
            e4.delete(0, END)
            e4.insert(0, a4)
 
            print(b50 * 50 + b20 * 20 + b10 * 10 + b5 * 5)
            shownp()
            status_label["text"] = "Done!"
            b1.configure(state="normal")
            b2.configure(state="normal")
            b3.configure(state="normal")
            b4.configure(state="normal")
 
        else:
            status_label["text"] = "Insert a number divisible by 5"
            e0.delete(0, END)
            e1.delete(0, END)
            e2.delete(0, END)
            e3.delete(0, END)
            e4.delete(0, END)
            b1.configure(state="disabled")
            b2.configure(state="disabled")
            b3.configure(state="disabled")
            b4.configure(state="disabled")
            e0.focus()
 
 
def change50(v1):
    print(v1)
    e1.delete(0, END)
    e2.delete(0, END)
    e3.delete(0, END)
    e4.delete(0, END)
    e1.insert(0, np[0][2] + v1)
    np[0][1] = 1
    callback()
 
 
def change20(v2):
    e1.delete(0, END)
    e2.delete(0, END)
    e3.delete(0, END)
    e4.delete(0, END)
    e2.insert(0, np[1][2] + v2)
    np[1][1] = 1
    callback()
 
 
def change10(v3):
    e1.delete(0, END)
    e2.delete(0, END)
    e3.delete(0, END)
    e4.delete(0, END)
    e3.insert(0, np[2][2] + v3)
    np[2][1] = 1
    callback()
 
 
def change5(v4):
    e1.delete(0, END)
    e2.delete(0, END)
    e3.delete(0, END)
    e4.delete(0, END)
    e3.insert(0, np[3][2] + v4)
    np[3][1] = 1
    callback()
 
 
def verifica():
    val1 = v1.get() * int(e1.get()) * 50
    val2 = v2.get() * int(e2.get()) * 20
    val3 = v3.get() * int(e3.get()) * 10
    val4 = v4.get() * int(e4.get()) * 5
    print(val1, val2, val3, val4)
    somma = val1 + val2 + val3 + val4
    print(somma)
    valtot = int(e0.get())
    if somma > valtot:
        b0.config(state="disabled")
        status_label[
            "text"
        ] = "Change the fixed number of banknotes because it exceeds the amount to be split!"
    else:
        b0.config(state=NORMAL)
 
 
Label(gui, text="Importo").place(x=5, y=5, width=50)
Label(gui, text="50,00 €").place(x=5, y=35, width=50)
Label(gui, text="20,00 €").place(x=5, y=65, width=50)
Label(gui, text="10,00 €").place(x=5, y=95, width=50)
Label(gui, text="5,00 €").place(x=5, y=125, width=50)
 
a1 = StringVar()
a2 = StringVar()
a3 = StringVar()
a4 = StringVar()
 
e0 = Entry(gui, validate="key", justify="center")
# e0["validatecommand"] = (e0.register(testVal), "%P", "%i", "%d")
 
e1 = Entry(gui, textvariable=a1, justify="center")
e2 = Entry(gui, textvariable=a2, justify="center")
e3 = Entry(gui, textvariable=a3, justify="center")
e4 = Entry(gui, textvariable=a4, justify="center")
 
# e0.grid(row=0, column=1)
e0.place(x=100, y=5, width=100)
e1.place(x=100, y=35, width=100)
e2.place(x=100, y=65, width=100)
e3.place(x=100, y=95, width=100)
e4.place(x=100, y=125, width=100)
 
b0 = Button(gui, text="Split", state="normal", command=callback)
b1 = Checkbutton(gui, text="fix", state="disabled", variable=v1, command=verifica)
b2 = Checkbutton(gui, text="fix", state="disabled", variable=v2, command=verifica)
b3 = Checkbutton(gui, text="fix", state="disabled", variable=v3, command=verifica)
b4 = Checkbutton(gui, text="fix", state="disabled", variable=v4, command=verifica)
b5 = Button(gui, text="Quit", command=gui.destroy)
 
b0.place(x=100, y=155, height=20, width=100)
b1.place(x=210, y=35, height=20, width=100)
b2.place(x=210, y=65, height=20, width=100)
b3.place(x=210, y=95, height=20, width=100)
b4.place(x=210, y=125, height=20, width=100)
b5.place(x=370, y=155, height=20, width=100)
 
status_label = Label(
    gui, height=3, width=35, bg="black", fg="#00FF00", text="---", wraplength=200
)
status_label.place(x=300, y=35, width=250)
 
e0.focus()
 
gui.mainloop()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Hi
Fantastic optimized code!
Thank's, DeaD_EyE, for your help.
Best Regards
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 2,125 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  [Tkinter] Tkinter don't change the image DQT 2 2,890 Jul-22-2022, 10:26 AM
Last Post: menator01
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 10,539 Jun-26-2022, 06:26 PM
Last Post: menator01
  Tkinter - How can I change the default Notebook border color? TurboC 5 17,678 May-23-2022, 03:44 PM
Last Post: bigmac
  Can't get tkinter button to change color based on changes in data dford 4 4,939 Feb-13-2022, 01:57 PM
Last Post: dford
  Tkinter Exit Code based on Entry Widget Nu2Python 6 6,428 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  tkinter change the text of the checkbox zazas321 1 5,412 Sep-17-2021, 06:19 AM
Last Post: zazas321
  tkinter text widget word wrap position chrisdb 6 10,585 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  Tkinter - How can I extend a label widget? TurboC 2 3,625 Oct-13-2020, 12:15 PM
Last Post: zazas321
  Tkinter menu font size -method to change tonycat 2 10,114 Oct-11-2020, 02:43 AM
Last Post: tonycat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020