Amazing-Python-Scripts

Форк
0
140 строк · 3.7 Кб
1
# import libraries
2
from tkinter import *
3

4
# initialized window
5
root = Tk()
6
root.geometry('480x350')
7
root.resizable(0, 0)
8
root.title('Weight Converter')
9

10
# defining the function for converting weights
11

12

13
def WeightConv():
14

15
    # making textbox user-friendly that is editable
16
    t1.configure(state='normal')
17
    t1.delete("1.0", END)
18

19
    t2.configure(state='normal')
20
    t2.delete("1.0", END)
21

22
    t3.configure(state='normal')
23
    t3.delete("1.0", END)
24

25
    t4.configure(state='normal')
26
    t4.delete("1.0", END)
27

28
    t5.configure(state='normal')
29
    t5.delete("1.0", END)
30

31
    t6.configure(state='normal')
32
    t6.delete("1.0", END)
33

34
    # exception handling
35
    try:
36
        kilograms = float(e1.get())
37

38
        # insert the output in textboxes correct upto 2 places after decimal
39
        t1.insert(END, "%.2f" % (kilograms * 5000))
40
        t2.insert(END, "%.2f" % (kilograms * 1000))
41
        t3.insert(END, "%.2f" % (kilograms * 35.274))
42
        t4.insert(END, "%.2f" % (kilograms * 2.20462))
43
        t5.insert(END, "%.2f" % (kilograms * 0.01))
44
        t6.insert(END, "%.2f" % (kilograms * 0.001))
45

46
    # if blank or invalid input is given then exception is thrown
47
    except ValueError:
48
        t1.insert(END, "  ~ Invalid input ~  ")
49
        t2.insert(END, "  ~ Invalid input ~  ")
50
        t3.insert(END, "  ~ Invalid input ~  ")
51
        t4.insert(END, "  ~ Invalid input ~  ")
52
        t5.insert(END, "  ~ Invalid input ~  ")
53
        t6.insert(END, "  ~ Invalid input ~  ")
54

55
    # making textbox uneditable
56
    t1.configure(state='disabled')
57
    t2.configure(state='disabled')
58
    t3.configure(state='disabled')
59
    t4.configure(state='disabled')
60
    t5.configure(state='disabled')
61
    t6.configure(state='disabled')
62

63

64
# creating a label to display
65
l1 = Label(root, text="Enter the weight in kilograms (kg) : ")
66
l1.grid(row=1, column=1, columnspan=2)
67
value = StringVar()
68

69
# creating a entry box for input
70
e1 = Entry(root, textvariable=value)
71
e1.grid(row=1, column=3, columnspan=2)
72

73
# create a button for conversion
74
button = Button(root, text="Convert", command=WeightConv)
75
button.grid(row=2, column=2, columnspan=2, rowspan=2)
76

77
# make labels for textbox
78
t1l1 = Label(root, text="kg to ct : ")
79
t1l1.grid(row=4, column=1, columnspan=1)
80

81
t2l2 = Label(root, text="kg to g : ")
82
t2l2.grid(row=5, column=1, columnspan=1)
83

84
t3l3 = Label(root, text="kg to oz : ")
85
t3l3.grid(row=6, column=1, columnspan=1)
86

87
t4l4 = Label(root, text="kg to lb : ")
88
t4l4.grid(row=7, column=1, columnspan=1)
89

90
t5l5 = Label(root, text="kg to q : ")
91
t5l5.grid(row=8, column=1, columnspan=1)
92

93
t6l6 = Label(root, text="kg to t : ")
94
t6l6.grid(row=9, column=1, columnspan=1)
95

96
t1r1 = Label(root, text="Carat")
97
t1r1.grid(row=4, column=4, columnspan=1)
98

99
t2r2 = Label(root, text="Gram")
100
t2r2.grid(row=5, column=4, columnspan=1)
101

102
t3r3 = Label(root, text="Ounce")
103
t3r3.grid(row=6, column=4, columnspan=1)
104

105
t4r4 = Label(root, text="Pound")
106
t4r4.grid(row=7, column=4, columnspan=1)
107

108
t5r5 = Label(root, text="Quintal")
109
t5r5.grid(row=8, column=4, columnspan=1)
110

111
t6r6 = Label(root, text="Tonne")
112
t6r6.grid(row=9, column=4, columnspan=1)
113

114
# creating textbox and defining grid to show output
115
t1 = Text(root, height=1, width=20)
116
t1.grid(row=4, column=2, columnspan=2)
117

118
t2 = Text(root, height=1, width=20)
119
t2.grid(row=5, column=2, columnspan=2)
120

121
t3 = Text(root, height=1, width=20)
122
t3.grid(row=6, column=2, columnspan=2)
123

124
t4 = Text(root, height=1, width=20)
125
t4.grid(row=7, column=2, columnspan=2)
126

127
t5 = Text(root, height=1, width=20)
128
t5.grid(row=8, column=2, columnspan=2)
129

130
t6 = Text(root, height=1, width=20)
131
t6.grid(row=9, column=2, columnspan=2)
132

133
# making blank spaces in GUI
134
for r in range(10):
135
    root.grid_rowconfigure(r, minsize=30)
136
for c in range(6):
137
    root.grid_columnconfigure(c, minsize=50)
138

139
# infinite loop to run program
140
root.mainloop()
141

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.