Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 203c1c9

Browse files
Merge pull request avinashkranjan#747 from mehabhalodiya/WeightConv
Added weight converter
2 parents c3ad8bf + c874c67 commit 203c1c9

File tree

2 files changed

+250
-0
lines changed

2 files changed

+250
-0
lines changed

‎Weight-Converter-GUI/README.md‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Weight Converter GUI
2+
3+
## Description
4+
5+
I have created a GUI based weight converter that accepts a kilogram input value and converts that value to Carat, Gram, Ounce, Pound, Quintal and Tonne (i.e. in decreasing order) when the user clicks the Convert button.
6+
7+
</br>
8+
9+
## Step 1: Creating a window Using Tk
10+
11+
The first step of any GUI is to create a window on which to begin placing components.
12+
13+
We first import everything from the `tkinter` module. We then create a variable called root which is of type `Tk()`. This will be the main window on which we place components later on. The title of the window is then set by calling the `title` function. Finally, the `mainloop()` function is called on root.
14+
15+
</br>
16+
17+
## Step 2: Adding a widget using grid
18+
19+
We can now start adding components, or widgets, to the window. The first widget we shall add will be `Label` widget. The basic construction of a `Label` widget is:
20+
21+
```
22+
Label(<parent>, text=<text>)
23+
```
24+
25+
Where `<parent>` is the parent object and `<text>` is a string which will be the text displayed. We will also need to add the `Label` to the grid and provide a column and row on which to place the widget on the grid.
26+
27+
```
28+
l1.grid(row = 1, column = 1)
29+
```
30+
31+
Now that we want to start placing widgets on a grid, the `Label` we have just created needs to be at column 1, row 1. Thus, when we place the Label on the grid, we provide the attributes column = 1 and row = 1.
32+
33+
This might not look as one would expect. The `Label` we have created is on the far left. The reason for this is because when the widgets are placed on the grid, any empty columns and rows are left empty without any indication of their existence. This means that, although the `Label` is actually in column 2, row 0, this will not be visually apparent until we add more widgets in the surrounding area.
34+
35+
</br>
36+
37+
## Step 3: Spanning widgets over multiple rows and columns
38+
39+
We can span several rows and columns of the grid. To achieve this behaviour, use the columnspan and rowspan attributes when placing a widget on the grid.
40+
41+
```
42+
button.grid(row = 2, column = 2, columnspan = 2, rowspan = 2)
43+
```
44+
45+
This code will place `Button` on the grid with the top corner at (2, 2) and spanning 2 columns, and 2 rows.
46+
47+
</br>
48+
49+
## Adding More Widgets
50+
51+
The `tkinter` module provides 11 different widgets, and there are an additional 6 widgets in the [tkinter.ttk](https://docs.python.org/3/library/tkinter.ttk.html) module. We will only need a few of these to complete our GUI, but the basic use of these widgets is the same.
52+
53+
</br>
54+
55+
### Entry
56+
57+
The `Entry` widget is used to allow the user to enter text. We will use the Entry widget for both the user input, and the output.
58+
59+
Basic `Entry` widgets are created in the following form:
60+
61+
```
62+
Entry(<parent>, textvariable=<variable>)
63+
```
64+
65+
Where `<parent>` is the container of the widget, and `<variable>` is a variable which will be bound to the widget. Not all widgets have the option to bind a variable to them.
66+
67+
When a variable is bound to an `Entry` widget, that variable will always be the value of the text inside the widget and hence the user's input. There are 4 possible types of variable which can be bound to widgets, `StringVar()`, `IntVar()`, `DoubleVar()` and `BooleanVar()`. Of which, not all types can always be used for all widgets.
68+
69+
```
70+
t1.configure(state='disabled')
71+
```
72+
73+
However, we will pass an additional attribute to the constructor of Entry, called `state`. The `state` attribute can take several forms including "active", "disabled", "pressed" and "selected", which define the current state of a widget. For the output Entry we will set its state to "disabled" to prevent the user from editing its contents.
74+
75+
```
76+
t1.configure(state = 'normal')
77+
```
78+
79+
This code would go in the callback for the event that will cause the Button to be enabled.
80+
81+
```
82+
t1.delete("1.0", END)
83+
```
84+
85+
This will delete from postion 0 till end.
86+
87+
</br>
88+
89+
### Button
90+
91+
Next we will add a Button to our GUI. Button widgets are constructed in the following way:
92+
93+
```
94+
Button(<parent>, text=<text>, command=<function>)
95+
```
96+
97+
Where <function> in the command attribute is the action taken when the Button is pressed.
98+
99+
</br>
100+
101+
## Additional feature:
102+
103+
### Exception Handling -
104+
105+
The `try` block lets you test a block of code for errors. The `except` block lets you handle the error.
106+
107+
Here, whenever a user enters any other value (i.e. alphabets, symbols etc.) instead of the interger or float(decimal) value than it will show a message saying "Invalid input" in the textbox.
108+
109+
</br>
110+
111+
## Output:
112+
113+
![wc](https://user-images.githubusercontent.com/73488906/112635679-1f6b5880-8e62-11eb-9b6b-8779f303defd.png)
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
def WeightConv():
12+
13+
# making textbox user-friendly that is editable
14+
t1.configure(state = 'normal')
15+
t1.delete("1.0", END)
16+
17+
t2.configure(state = 'normal')
18+
t2.delete("1.0", END)
19+
20+
t3.configure(state = 'normal')
21+
t3.delete("1.0", END)
22+
23+
t4.configure(state='normal')
24+
t4.delete("1.0", END)
25+
26+
t5.configure(state='normal')
27+
t5.delete("1.0", END)
28+
29+
t6.configure(state='normal')
30+
t6.delete("1.0", END)
31+
32+
# exception handling
33+
try:
34+
kilograms = float(e1.get())
35+
36+
# insert the output in textboxes correct upto 2 places after decimal
37+
t1.insert(END, "%.2f" % (kilograms * 5000))
38+
t2.insert(END, "%.2f" % (kilograms * 1000))
39+
t3.insert(END, "%.2f" % (kilograms * 35.274))
40+
t4.insert(END, "%.2f" % (kilograms * 2.20462))
41+
t5.insert(END, "%.2f" % (kilograms * 0.01))
42+
t6.insert(END, "%.2f" % (kilograms * 0.001))
43+
44+
# if blank or invalid input is given then exception is thrown
45+
except ValueError:
46+
t1.insert(END, " ~ Invalid input ~ ")
47+
t2.insert(END, " ~ Invalid input ~ ")
48+
t3.insert(END, " ~ Invalid input ~ ")
49+
t4.insert(END, " ~ Invalid input ~ ")
50+
t5.insert(END, " ~ Invalid input ~ ")
51+
t6.insert(END, " ~ Invalid input ~ ")
52+
53+
# making textbox uneditable
54+
t1.configure(state='disabled')
55+
t2.configure(state='disabled')
56+
t3.configure(state='disabled')
57+
t4.configure(state='disabled')
58+
t5.configure(state='disabled')
59+
t6.configure(state='disabled')
60+
61+
# creating a label to display
62+
l1 = Label(root, text = "Enter the weight in kilograms (kg) : ")
63+
l1.grid(row = 1, column = 1,columnspan = 2)
64+
value = StringVar()
65+
66+
# creating a entry box for input
67+
e1 = Entry(root, textvariable=value)
68+
e1.grid(row = 1, column = 3,columnspan = 2)
69+
70+
# create a button for conversion
71+
button = Button(root, text = "Convert", command = WeightConv)
72+
button.grid(row = 2, column = 2, columnspan = 2, rowspan = 2)
73+
74+
# make labels for textbox
75+
t1l1 = Label(root, text = "kg to ct : ")
76+
t1l1.grid(row = 4, column = 1, columnspan=1)
77+
78+
t2l2 = Label(root, text = "kg to g : ")
79+
t2l2.grid(row = 5, column = 1, columnspan=1)
80+
81+
t3l3 = Label(root, text = "kg to oz : ")
82+
t3l3.grid(row = 6, column = 1, columnspan=1)
83+
84+
t4l4 = Label(root, text = "kg to lb : ")
85+
t4l4.grid(row = 7, column = 1, columnspan=1)
86+
87+
t5l5 = Label(root, text = "kg to q : ")
88+
t5l5.grid(row = 8, column = 1, columnspan=1)
89+
90+
t6l6 = Label(root, text = "kg to t : ")
91+
t6l6.grid(row = 9, column = 1, columnspan=1)
92+
93+
t1r1 = Label(root, text = "Carat")
94+
t1r1.grid(row = 4, column = 4, columnspan=1)
95+
96+
t2r2 = Label(root, text = "Gram")
97+
t2r2.grid(row = 5, column = 4, columnspan=1)
98+
99+
t3r3 = Label(root, text = "Ounce")
100+
t3r3.grid(row = 6, column = 4, columnspan=1)
101+
102+
t4r4 = Label(root, text = "Pound")
103+
t4r4.grid(row = 7, column = 4, columnspan=1)
104+
105+
t5r5 = Label(root, text = "Quintal")
106+
t5r5.grid(row = 8, column = 4, columnspan=1)
107+
108+
t6r6 = Label(root, text = "Tonne")
109+
t6r6.grid(row = 9, column = 4, columnspan=1)
110+
111+
# creating textbox and defining grid to show output
112+
t1 = Text(root, height = 1, width = 20)
113+
t1.grid(row = 4, column = 2, columnspan = 2)
114+
115+
t2 = Text(root, height = 1, width = 20)
116+
t2.grid(row = 5, column = 2, columnspan = 2)
117+
118+
t3 = Text(root, height = 1, width = 20)
119+
t3.grid(row = 6, column = 2, columnspan = 2)
120+
121+
t4 = Text(root, height = 1, width = 20)
122+
t4.grid(row = 7, column = 2, columnspan = 2)
123+
124+
t5 = Text(root, height = 1, width = 20)
125+
t5.grid(row = 8, column = 2, columnspan = 2)
126+
127+
t6 = Text(root, height = 1, width = 20)
128+
t6.grid(row = 9, column = 2, columnspan = 2)
129+
130+
# making blank spaces in GUI
131+
for r in range(10):
132+
root.grid_rowconfigure(r,minsize = 30)
133+
for c in range(6):
134+
root.grid_columnconfigure(c,minsize = 50)
135+
136+
# infinite loop to run program
137+
root.mainloop()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /