Python - Convert String To Integer Type
Submitted by razormist on Tuesday, October 15, 2019 - 13:34.
In this tutorial we will create a Convert String To Integer Type using Python. This code will convert the given string into integer data type when user click the convert button. The code use tkinter module to create a layout and widgets that can call python functions. When a function is called it the application will automatically convert the given string data type into integer data type using convert().
We will be using Python because it has a design philosophy which emphasizes code readability. That's why python is very easy to use especially for beginners who just started programming. It is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing..
There you have it we just created a Convert String To Integer Type Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Getting started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.Importing Modules
After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor.- from tkinter import *
Setting up the Main Frame
After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.- #==================================MAIN FRAME=========================================
- root = Tk()
- root.title("Python - Convert String To Integer Type")
- width=450
- height=250
- screen_width=root.winfo_screenwidth()
- screen_height=root.winfo_screenheight()
- x=(screen_width/2) - (width/2)
- y=(screen_height/2) - (height/2)
- root.geometry("%dx%d+%d+%d" % (width, height, x, y))
- root.resizable(0, 0)
Designing the Layout
After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.- #=================================VARIABLES===========================================
- NUMBER = StringVar()
- #=================================FRAMES==============================================
- Top = Frame(root, relief=SOLID, bd=1)
- Top.pack(fill=X)
- Mid = Frame(root)
- Mid.pack(pady=20)
- Left = Frame(Mid)
- Left.pack(side=LEFT)
- Right = Frame(Mid)
- Right.pack(side=RIGHT)
- Bottom = Frame(root)
- Bottom.pack()
- #=================================LABEL WIDGETS=======================================
- lbl_title = Label(Top, font=('calibri', 18), text="Python - Convert String To Integer Type")
- lbl_title.pack()
- lbl_text = Label(Left, text="Enter a string number", font=('calibri', 14))
- lbl_text.pack()
- lbl_text2 = Label(Bottom, text="Display Result", font=('calibri', 14))
- lbl_text2.pack(side=TOP)
- lbl_result = Label(Bottom, font=('calibri', 12))
- lbl_result.pack()
- #=================================ENTRY WIDGETS=======================================
- txt_str = Entry(Right, font=('calibri', 14), textvariable=NUMBER)
- txt_str.pack()
- #=================================BUTTON WIDGETS======================================
- btn_convert = Button(Bottom, text="Convert", command=convert)
- btn_convert.pack(pady=10)
Creating the Main Function
This is where the code that contains the main functions. This code will convert the string into a readable integer data type when the button is clicked. To do that just copy and write these blocks of code.- #=================================METHODS=============================================
- def convert():
- if NUMBER.get() == "":
- lbl_result.config(text="Please enter a string", fg="yellow")
- else:
- if num_verify(NUMBER.get()):
- integer = int(NUMBER.get())
- lbl_result.config(text="\"" + NUMBER.get() + "\" is a string, will be converted to " + str(integer) + " Integer type", fg="blue")
- else:
- lbl_result.config(text="This is not a string number", fg="Red")
- def num_verify(s):
- try:
- float(s)
- return True
- except ValueError:
- pass
- try:
- import unicodedata
- unicodedata.numeric(s)
- return True
- except (TypeError, ValueError):
- pass
- return False
Initializing the Application
After finishing the function save the application as index.py. This function will run the code and check if the main application is initialized properly. To do that copy the code below and paste it inside the IDLE text editor.- #=================================INITIALIZATION======================================
- if __name__ == '__main__':
- root.mainloop()
Add new comment
- 196 views