My Journey into the Jungle of the Python Part 2 : Uncategorized - Hallo sahabat Computer Programmer Medical Coding, Pada Artikel yang anda baca kali ini dengan judul My Journey into the Jungle of the Python Part 2 : Uncategorized, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan
Artikel Beginners,
Artikel Python, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.
Judul : My Journey into the Jungle of the Python Part 2 : Uncategorized
link : My Journey into the Jungle of the Python Part 2 : Uncategorized
I’m going to be starting school tomorrow. My updates will be less frequent.
Artikel yang anda baca saat ini My Journey into the Jungle of the Python Part 2 : Uncategorized dengan alamat link https://idkingcode.blogspot.com/2016/08/my-journey-into-jungle-of-python-part-2.html
Judul : My Journey into the Jungle of the Python Part 2 : Uncategorized
link : My Journey into the Jungle of the Python Part 2 : Uncategorized
My Journey into the Jungle of the Python Part 2 : Uncategorized
I’m going to be starting school tomorrow. My updates will be less frequent.
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
Running it should give this as a result:
The first thing that happens is that Tkinter and all of its functions are imported in from Tkinter import *. Then, root = Tk() creates the root widget, a basic window. A “widget” is a component of a GUI. w = Label(root, text="Hello, world!") Creates a label class that is the child of root. It displays the text “Hello There” on the window. w.pack() “packs” the widget, which tells it to size itself to fit the given text, and make itself visible.
Finally, after all that, root.mainloop() initiates the opening of the window. It launches into a loop that can only be terminated by closing out of the window.
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
Running it should give this as a result:
The first thing that happens is that Tkinter and all of its functions are imported in from Tkinter import *. Then, root = Tk() creates the root widget, a basic window. A “widget” is a component of a GUI. w = Label(root, text="Hello, world!") Creates a label class that is the child of root. It displays the text “Hello There” on the window. w.pack() “packs” the widget, which tells it to size itself to fit the given text, and make itself visible.
Finally, after all that, root.mainloop() initiates the opening of the window. It launches into a loop that can only be terminated by closing out of the window.
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(frame, text="QUIT", fg="red", bg="black", command=frame.quit)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print "Hello, World!"
root = Tk()
app = App(root)
root.mainloop()
The result:
Let’s begin with some lines down near the end to make things seem clearer:
root = Tk()
app = App(root)
The root widget is initialized, and stored in class App as app.
def __init__(self, master):
frame = Frame(master)
frame.pack()
The app object is initialized. A “frame” for “root” is created. A “frame” is a type of container to hold widgets in.
self.button = Button(frame, text="QUIT", fg="red", bg="black", command=frame.quit)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
The first two lines of code create a button widget that is the child of the frame widget in (frame,. The text printed is “QUIT”, the foreground of the button, the text, is red, the background is black, and when the button is pressed, the GUI closes. because of the frame.quit command. self.button.pack(side=LEFT) packs the button to the left center side of the frame.
The next two lines are very similar to the first, except the command self.say_hi takes us to:
def say_hi(self):
print "Hello, World!"
where when you click on the button, “Hello World!” is printed.
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print "Hello, World!"
root = Tk()
app = App(root)
root.mainloop()
The result:
Let’s begin with some lines down near the end to make things seem clearer:
root = Tk()
app = App(root)
The root widget is initialized, and stored in class App as app.
def __init__(self, master):
frame = Frame(master)
frame.pack()
The app object is initialized. A “frame” for “root” is created. A “frame” is a type of container to hold widgets in.
self.button = Button(frame, text="QUIT", fg="red", bg="black", command=frame.quit)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
The first two lines of code create a button widget that is the child of the frame widget in (frame,. The text printed is “QUIT”, the foreground of the button, the text, is red, the background is black, and when the button is pressed, the GUI closes. because of the frame.quit command. self.button.pack(side=LEFT) packs the button to the left center side of the frame.
The next two lines are very similar to the first, except the command self.say_hi takes us to:
def say_hi(self):
print "Hello, World!"
where when you click on the button, “Hello World!” is printed.
Before I went into GUI’s, I should have covered Object-Oriented programming. Object-Oriented Programming is programming centered around “objects”, which are combined pieces of data and functions. What we have been doing before is procedure-oriented, which is designed around functions and blocks of statements. It is very useful for complex programming such as games.
Type in the following:
class Printer:
def print_something (self, what):
print "My name is", what
printobj = Printer ()
printobj.print_something ("Yuval")
Type in the following:
class Printer:
def print_something (self, what):
print "My name is", what
printobj = Printer ()
printobj.print_something ("Yuval")
How it Works:
First, class Printer defines the “class”, or “type” Printer. A class is a mold for creating objects. It is very similar to a function except that it requires an extra parameter, the “self” parameter. This parameter defined as the object, “printobj”, when “printobj.print_something (”Yuval”)” is run. The “what” perimeter is defined as an unknown variable in the function print_something. It can only be defined in “printobj.print_something (”Yuval”)”. “printobj = Printer ()” defines an object “Printer ()”. The statement “printobj.print_something (”Yuval”)” puts “printobj” as self and “Hello There” as “what”. Generally, the brackets after a method, a function of a class, is where you put the definition of the “what” perimeter.
class Printer:
def print_something (self, what):
print "My name is", what
printobj = Printer ()
printobj.print_something ("Yuval")
class BetterPrinter (Printer):
def printname (self, what):
self.thing_we_print = what
print self.thing_we_print
bprinter = BetterPrinter ()
bprinter.print_something ("Jack")
bprinter.printname ("Jack")
This is an extension of the previous program with a new class, the BetterPrinter class. Note that in the statement “bprinter.print_something (”Jack”)” “print_something” was never defined in BetterPrinter. That’s because the line class BetterPrinter (Printer): includes all the functions from the “Printer” class. This is called inheritance. Also, in
def printname (self, what):
self.thing_we_print = what
print self.thing_we_print
the variable self.thing_we_print has the “self” in front because each object should have its own “thing_we_print”.
Finally, there is the __init__ method. It is a special kind of method.
class Person:
def __init__(self, name):
self.name = name
def say_hi(self):
print 'Hello, my name is', self.name
Person('Yuval').say_hi()
“Person(’Yuval’)” doesn’t need to be appended to __init__ because __init__ automatically initializes “Person(’Yuval’)”, meaning that “Person(’Yuval’)” is already run through the function “__init__(self, name)”, so ‘Yuval’ is defined as ’self.name’ automatically. Then, “Hello my name is Yuval” is printed out.
Now, I will rewrite the Tkinter tutorials with added OOP.
First, class Printer defines the “class”, or “type” Printer. A class is a mold for creating objects. It is very similar to a function except that it requires an extra parameter, the “self” parameter. This parameter defined as the object, “printobj”, when “printobj.print_something (”Yuval”)” is run. The “what” perimeter is defined as an unknown variable in the function print_something. It can only be defined in “printobj.print_something (”Yuval”)”. “printobj = Printer ()” defines an object “Printer ()”. The statement “printobj.print_something (”Yuval”)” puts “printobj” as self and “Hello There” as “what”. Generally, the brackets after a method, a function of a class, is where you put the definition of the “what” perimeter.
class Printer:
def print_something (self, what):
print "My name is", what
printobj = Printer ()
printobj.print_something ("Yuval")
class BetterPrinter (Printer):
def printname (self, what):
self.thing_we_print = what
print self.thing_we_print
bprinter = BetterPrinter ()
bprinter.print_something ("Jack")
bprinter.printname ("Jack")
This is an extension of the previous program with a new class, the BetterPrinter class. Note that in the statement “bprinter.print_something (”Jack”)” “print_something” was never defined in BetterPrinter. That’s because the line class BetterPrinter (Printer): includes all the functions from the “Printer” class. This is called inheritance. Also, in
def printname (self, what):
self.thing_we_print = what
print self.thing_we_print
the variable self.thing_we_print has the “self” in front because each object should have its own “thing_we_print”.
Finally, there is the __init__ method. It is a special kind of method.
class Person:
def __init__(self, name):
self.name = name
def say_hi(self):
print 'Hello, my name is', self.name
Person('Yuval').say_hi()
“Person(’Yuval’)” doesn’t need to be appended to __init__ because __init__ automatically initializes “Person(’Yuval’)”, meaning that “Person(’Yuval’)” is already run through the function “__init__(self, name)”, so ‘Yuval’ is defined as ’self.name’ automatically. Then, “Hello my name is Yuval” is printed out.
Now, I will rewrite the Tkinter tutorials with added OOP.
Demikianlah Artikel : My Journey into the Jungle of the Python Part 2 : Uncategorized
Semoga artikel My Journey into the Jungle of the Python Part 2 : Uncategorized ini, bisa memberi manfaat untuk anda semua dan jangan bosan untuk datang kembali mengunjungi blog ini. Baiklah, sampai jumpa di postingan artikel lainnya.
Artikel yang anda baca saat ini My Journey into the Jungle of the Python Part 2 : Uncategorized dengan alamat link https://idkingcode.blogspot.com/2016/08/my-journey-into-jungle-of-python-part-2.html
My Journey into the Jungle of the Python Part 2 : Uncategorized
4/
5
Oleh
Unknown
