![]() |
Python guide [Basics & Gui] - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: Python guide [Basics & Gui] (/Thread-Python-guide-Basics-Gui) Pages:
1
2
|
Python guide [Basics & Gui] - Guxi - 01-14-2013 Tutorial for python beginners. ![]() my experience in python programming If you are new to programming python should be great choice for you, but it also comes with few bad things. Python is good because it's kinda lightweight, simple, understandable syntaxes, huge library of modules and extensions... Python is possible to understand even if you aren't python programmer, because it's kinda familiar with pseudo. What's bad in python? Well windows, if you want to make a program that is predicted to be ran on windows you will have to pack a python library with your application because python is not installed on windows by default. So there you might come a cross some problems. Most popular 'compilers' are py2exe,pyinstaller and maybe cython( which works with c++, but 'm not familiar with it). I recommend you to install python 2.7.3v because it's most popular, used cyberwide in py programming. In this tutorial i will introduce you with basics of python and python gui development. Making your first 'Hello World' program. Code: print "Hello World" Now there is 3 types of value that you will need to know: string It's always stored in " " If you wrote Code: print Hello World integer example of integer is Code: 4 Booleans They can be only True or False. In programming you will have to get a true or false for many things. For example, if you need to check does a specific file exists, if it does exist, your code will return you statement: True. Variables You use them to store some kind of value in them. They are like backpacks which are carrying some kind of value (string,int,boolean). To declare a variable and assign a value to it you need to do following: Code: my_variable = "Hello World" Variables are one of most important things in python, so you need to use it everywhere. Now if you want to display/print value of variable called my variable, you will have to do following: Code: print my_variable Get input from user For getting input from user you will need to use Code: raw_input() Now variables will help you. To save user's input from raw_input() command, you will need to store them in variable. Code: my_variable = raw_input() Now my_variable will get new value, depending on what user writes as input to program. To display users input you will need to print variable that you declared with raw_input() in this case: Code: print my_variable variables example I'm gonna show you how to ask user his name,age,location and then we will display it to him in same sentence. Your mission is to ask user 3 questions what's his name, age and location. Code: name = raw_input("Your name: ") Now you might be asking what str() does? Well it will convert anything inside of brackets to string. In this case we were expecting user to enter the int type of value. There is also int() which can convert "5" to 5 so you could do math with it for example. playing with strings You can replace specific part of strings. For example "How to learn hacking?" If you want to remove "how to" part you can do following: Code: word = "How to learn hacking?" Code above will print you "learn hacking?" You replaced string "How to" with "" ( nothing, no chars = nothing ) IF statement In programming you need to make more space for possibilities and alternatives. If statement will allow you to make unlimited number of alternatives. This is how if statement works in theory: Code: if expression: ![]() For constructing the expression, you will commonly use Code: > expressions We are going to ask user to give us number between 1 and 10, if he doesn't give us number in that range, we print him msg that it's not in range 1-10. Code: print "enter any number in range 1-10" We used and because we need to make sure that it's not higher than 10 and lower than 0. So we need to check for 2 things. Imagine if you had to make 2 if statements, one checks is it greater than 10 and other one checks if it's lower than 0. That's why and helps so much. Ask user for password Now user will have to type correct password in order to proceed to next part of program. Code: password = "aeiou" # we are declaring variable in which we are storing the password else, you will get "wrong password" note: don't use this kind of password protection if it's important for you Functions Functions are very important, because it makes more functional :epic:. Theres no program without a single function. ( there is but non-practical ones) to define a function you will use def, also everything inside a function must be in indentation. Code: def my_function(): As you see every function has brackets, which means that they are capable of taking some arguments.( will get to that later) To call/start a function you need to do following Code: my_function() for example: Code: def welcome(name): You would get output with "welcome to function name_you_entered You might be asking why couldn't we just do Code: print "welcome to function " answer Code: print "welcome to function " name Well it's because answer is not global variable, so we have to 'import' it to function by giving it arguments. So inside of functions you can put anything but make sure that it's in indentation. Modules those are important because they expand possibilities of your program. you import one by doing following: Code: import [i]module_name[/i] Module that provides you GUI library is called tkinter, so you would write: Code: import Tkinter Python graphical user interface gui library that we are using will be Tkinter. It's simple and easy! Make sure you import the module for GUI Code: import Tkinter First thing that we could possibly do is to define a master/parent window. We will use name "root" for master window. Code: import Tkinter # GUI module imported It makes sure that your window doesn't destroy, it needs to loop. Same thing if you do Code: print "hello world" ![]() Now we made a simplest gui program, that will make a empty window. But now let's go further! Let's add title and geometry to our program! Code: import Tkinter # GUI module imported Adding widgets All the things that will replace your CLI commands to new one using Tk module label This will actually replace the print in CLI. Label is way to output a value to user. Now you have to define the label: Code: mytext = Label(root,text="HackCommunity",bg="black",fg="red") That's the only thing that must be first. ___label config______ text will display "HackCommunity" text bg Color of label's background fg Color of text _______________ mytext.pack() Will actually pack your widgets like in tetris, to the first spot available. You could also use .grid() .place() <- this one is used for navigating your widget by pixels. Entry Used instead of raw_input() or input(). Code: box = Entry(root, width="30") width of entry box is set to 30 pixels. Button Button is very important, because that's the most common way of starting a function or similar.. Code: my_button = Button(root,text="Click here",fg="green",borderwidth="0") Text inside of button is "Click here" font color is green, and there is border on the button, because we set it 0. Making program GUI functional To activate a function by button you will need to add command= property to button. Code: def myfunc(): Now when you click on the button, you will activate myfunc function, and it will create a new label with text "Hello". Now let's add Entry to the same code. We will ask user to write his name in entry box: Code: def myfunc(): [username] I hope you enjoy this tutorial! Tutorial is written by me 100%, i will add some more stuff here but this is enough for start. I invested my time for new programmers, Rep appreciated Thanks! Will be updated Python guide [Basics & Gui] - Guxi - 01-14-2013 Tutorial for python beginners. ![]() my experience in python programming If you are new to programming python should be great choice for you, but it also comes with few bad things. Python is good because it's kinda lightweight, simple, understandable syntaxes, huge library of modules and extensions... Python is possible to understand even if you aren't python programmer, because it's kinda familiar with pseudo. What's bad in python? Well windows, if you want to make a program that is predicted to be ran on windows you will have to pack a python library with your application because python is not installed on windows by default. So there you might come a cross some problems. Most popular 'compilers' are py2exe,pyinstaller and maybe cython( which works with c++, but 'm not familiar with it). I recommend you to install python 2.7.3v because it's most popular, used cyberwide in py programming. In this tutorial i will introduce you with basics of python and python gui development. Making your first 'Hello World' program. Code: print "Hello World" Now there is 3 types of value that you will need to know: string It's always stored in " " If you wrote Code: print Hello World integer example of integer is Code: 4 Booleans They can be only True or False. In programming you will have to get a true or false for many things. For example, if you need to check does a specific file exists, if it does exist, your code will return you statement: True. Variables You use them to store some kind of value in them. They are like backpacks which are carrying some kind of value (string,int,boolean). To declare a variable and assign a value to it you need to do following: Code: my_variable = "Hello World" Variables are one of most important things in python, so you need to use it everywhere. Now if you want to display/print value of variable called my variable, you will have to do following: Code: print my_variable Get input from user For getting input from user you will need to use Code: raw_input() Now variables will help you. To save user's input from raw_input() command, you will need to store them in variable. Code: my_variable = raw_input() Now my_variable will get new value, depending on what user writes as input to program. To display users input you will need to print variable that you declared with raw_input() in this case: Code: print my_variable variables example I'm gonna show you how to ask user his name,age,location and then we will display it to him in same sentence. Your mission is to ask user 3 questions what's his name, age and location. Code: name = raw_input("Your name: ") Now you might be asking what str() does? Well it will convert anything inside of brackets to string. In this case we were expecting user to enter the int type of value. There is also int() which can convert "5" to 5 so you could do math with it for example. playing with strings You can replace specific part of strings. For example "How to learn hacking?" If you want to remove "how to" part you can do following: Code: word = "How to learn hacking?" Code above will print you "learn hacking?" You replaced string "How to" with "" ( nothing, no chars = nothing ) IF statement In programming you need to make more space for possibilities and alternatives. If statement will allow you to make unlimited number of alternatives. This is how if statement works in theory: Code: if expression: ![]() For constructing the expression, you will commonly use Code: > expressions We are going to ask user to give us number between 1 and 10, if he doesn't give us number in that range, we print him msg that it's not in range 1-10. Code: print "enter any number in range 1-10" We used and because we need to make sure that it's not higher than 10 and lower than 0. So we need to check for 2 things. Imagine if you had to make 2 if statements, one checks is it greater than 10 and other one checks if it's lower than 0. That's why and helps so much. Ask user for password Now user will have to type correct password in order to proceed to next part of program. Code: password = "aeiou" # we are declaring variable in which we are storing the password else, you will get "wrong password" note: don't use this kind of password protection if it's important for you Functions Functions are very important, because it makes more functional :epic:. Theres no program without a single function. ( there is but non-practical ones) to define a function you will use def, also everything inside a function must be in indentation. Code: def my_function(): As you see every function has brackets, which means that they are capable of taking some arguments.( will get to that later) To call/start a function you need to do following Code: my_function() for example: Code: def welcome(name): You would get output with "welcome to function name_you_entered You might be asking why couldn't we just do Code: print "welcome to function " answer Code: print "welcome to function " name Well it's because answer is not global variable, so we have to 'import' it to function by giving it arguments. So inside of functions you can put anything but make sure that it's in indentation. Modules those are important because they expand possibilities of your program. you import one by doing following: Code: import [i]module_name[/i] Module that provides you GUI library is called tkinter, so you would write: Code: import Tkinter Python graphical user interface gui library that we are using will be Tkinter. It's simple and easy! Make sure you import the module for GUI Code: import Tkinter First thing that we could possibly do is to define a master/parent window. We will use name "root" for master window. Code: import Tkinter # GUI module imported It makes sure that your window doesn't destroy, it needs to loop. Same thing if you do Code: print "hello world" ![]() Now we made a simplest gui program, that will make a empty window. But now let's go further! Let's add title and geometry to our program! Code: import Tkinter # GUI module imported Adding widgets All the things that will replace your CLI commands to new one using Tk module label This will actually replace the print in CLI. Label is way to output a value to user. Now you have to define the label: Code: mytext = Label(root,text="HackCommunity",bg="black",fg="red") That's the only thing that must be first. ___label config______ text will display "HackCommunity" text bg Color of label's background fg Color of text _______________ mytext.pack() Will actually pack your widgets like in tetris, to the first spot available. You could also use .grid() .place() <- this one is used for navigating your widget by pixels. Entry Used instead of raw_input() or input(). Code: box = Entry(root, width="30") width of entry box is set to 30 pixels. Button Button is very important, because that's the most common way of starting a function or similar.. Code: my_button = Button(root,text="Click here",fg="green",borderwidth="0") Text inside of button is "Click here" font color is green, and there is border on the button, because we set it 0. Making program GUI functional To activate a function by button you will need to add command= property to button. Code: def myfunc(): Now when you click on the button, you will activate myfunc function, and it will create a new label with text "Hello". Now let's add Entry to the same code. We will ask user to write his name in entry box: Code: def myfunc(): [username] I hope you enjoy this tutorial! Tutorial is written by me 100%, i will add some more stuff here but this is enough for start. I invested my time for new programmers, Rep appreciated Thanks! Will be updated Python guide [Basics & Gui] - Guxi - 01-14-2013 Tutorial for python beginners. ![]() my experience in python programming If you are new to programming python should be great choice for you, but it also comes with few bad things. Python is good because it's kinda lightweight, simple, understandable syntaxes, huge library of modules and extensions... Python is possible to understand even if you aren't python programmer, because it's kinda familiar with pseudo. What's bad in python? Well windows, if you want to make a program that is predicted to be ran on windows you will have to pack a python library with your application because python is not installed on windows by default. So there you might come a cross some problems. Most popular 'compilers' are py2exe,pyinstaller and maybe cython( which works with c++, but 'm not familiar with it). I recommend you to install python 2.7.3v because it's most popular, used cyberwide in py programming. In this tutorial i will introduce you with basics of python and python gui development. Making your first 'Hello World' program. Code: print "Hello World" Now there is 3 types of value that you will need to know: string It's always stored in " " If you wrote Code: print Hello World integer example of integer is Code: 4 Booleans They can be only True or False. In programming you will have to get a true or false for many things. For example, if you need to check does a specific file exists, if it does exist, your code will return you statement: True. Variables You use them to store some kind of value in them. They are like backpacks which are carrying some kind of value (string,int,boolean). To declare a variable and assign a value to it you need to do following: Code: my_variable = "Hello World" Variables are one of most important things in python, so you need to use it everywhere. Now if you want to display/print value of variable called my variable, you will have to do following: Code: print my_variable Get input from user For getting input from user you will need to use Code: raw_input() Now variables will help you. To save user's input from raw_input() command, you will need to store them in variable. Code: my_variable = raw_input() Now my_variable will get new value, depending on what user writes as input to program. To display users input you will need to print variable that you declared with raw_input() in this case: Code: print my_variable variables example I'm gonna show you how to ask user his name,age,location and then we will display it to him in same sentence. Your mission is to ask user 3 questions what's his name, age and location. Code: name = raw_input("Your name: ") Now you might be asking what str() does? Well it will convert anything inside of brackets to string. In this case we were expecting user to enter the int type of value. There is also int() which can convert "5" to 5 so you could do math with it for example. playing with strings You can replace specific part of strings. For example "How to learn hacking?" If you want to remove "how to" part you can do following: Code: word = "How to learn hacking?" Code above will print you "learn hacking?" You replaced string "How to" with "" ( nothing, no chars = nothing ) IF statement In programming you need to make more space for possibilities and alternatives. If statement will allow you to make unlimited number of alternatives. This is how if statement works in theory: Code: if expression: ![]() For constructing the expression, you will commonly use Code: > expressions We are going to ask user to give us number between 1 and 10, if he doesn't give us number in that range, we print him msg that it's not in range 1-10. Code: print "enter any number in range 1-10" We used and because we need to make sure that it's not higher than 10 and lower than 0. So we need to check for 2 things. Imagine if you had to make 2 if statements, one checks is it greater than 10 and other one checks if it's lower than 0. That's why and helps so much. Ask user for password Now user will have to type correct password in order to proceed to next part of program. Code: password = "aeiou" # we are declaring variable in which we are storing the password else, you will get "wrong password" note: don't use this kind of password protection if it's important for you Functions Functions are very important, because it makes more functional :epic:. Theres no program without a single function. ( there is but non-practical ones) to define a function you will use def, also everything inside a function must be in indentation. Code: def my_function(): As you see every function has brackets, which means that they are capable of taking some arguments.( will get to that later) To call/start a function you need to do following Code: my_function() for example: Code: def welcome(name): You would get output with "welcome to function name_you_entered You might be asking why couldn't we just do Code: print "welcome to function " answer Code: print "welcome to function " name Well it's because answer is not global variable, so we have to 'import' it to function by giving it arguments. So inside of functions you can put anything but make sure that it's in indentation. Modules those are important because they expand possibilities of your program. you import one by doing following: Code: import [i]module_name[/i] Module that provides you GUI library is called tkinter, so you would write: Code: import Tkinter Python graphical user interface gui library that we are using will be Tkinter. It's simple and easy! Make sure you import the module for GUI Code: import Tkinter First thing that we could possibly do is to define a master/parent window. We will use name "root" for master window. Code: import Tkinter # GUI module imported It makes sure that your window doesn't destroy, it needs to loop. Same thing if you do Code: print "hello world" ![]() Now we made a simplest gui program, that will make a empty window. But now let's go further! Let's add title and geometry to our program! Code: import Tkinter # GUI module imported Adding widgets All the things that will replace your CLI commands to new one using Tk module label This will actually replace the print in CLI. Label is way to output a value to user. Now you have to define the label: Code: mytext = Label(root,text="HackCommunity",bg="black",fg="red") That's the only thing that must be first. ___label config______ text will display "HackCommunity" text bg Color of label's background fg Color of text _______________ mytext.pack() Will actually pack your widgets like in tetris, to the first spot available. You could also use .grid() .place() <- this one is used for navigating your widget by pixels. Entry Used instead of raw_input() or input(). Code: box = Entry(root, width="30") width of entry box is set to 30 pixels. Button Button is very important, because that's the most common way of starting a function or similar.. Code: my_button = Button(root,text="Click here",fg="green",borderwidth="0") Text inside of button is "Click here" font color is green, and there is border on the button, because we set it 0. Making program GUI functional To activate a function by button you will need to add command= property to button. Code: def myfunc(): Now when you click on the button, you will activate myfunc function, and it will create a new label with text "Hello". Now let's add Entry to the same code. We will ask user to write his name in entry box: Code: def myfunc(): [username] I hope you enjoy this tutorial! Tutorial is written by me 100%, i will add some more stuff here but this is enough for start. I invested my time for new programmers, Rep appreciated Thanks! Will be updated RE: Python guide [Basics & Gui] - bluedog.tar.gz - 01-14-2013 Very nice thread, you explained it pretty well. A hint, mayby some images would work. Example of this IF statement. ![]() Other then that +1 RE: Python guide [Basics & Gui] - bluedog.tar.gz - 01-14-2013 Very nice thread, you explained it pretty well. A hint, mayby some images would work. Example of this IF statement. ![]() Other then that +1 RE: Python guide [Basics & Gui] - bluedog.tar.gz - 01-14-2013 Very nice thread, you explained it pretty well. A hint, mayby some images would work. Example of this IF statement. ![]() Other then that +1 RE: Python guide [Basics & Gui] - LightX - 01-14-2013 Great tutorial buddy! Can't wait to see more from you! RE: Python guide [Basics & Gui] - LightX - 01-14-2013 Great tutorial buddy! Can't wait to see more from you! RE: Python guide [Basics & Gui] - LightX - 01-14-2013 Great tutorial buddy! Can't wait to see more from you! RE: Python guide [Basics & Gui] - Guxi - 01-14-2013 Good idea about images, also, i'm glad to hear that Lightx ![]() |