SecondVersion
This commit is contained in:
parent
bf5613b863
commit
20dc441817
|
@ -1184,6 +1184,18 @@ while counter <= 10:
|
||||||
|
|
||||||
print("after the loop, counter: {}".format(counter))
|
print("after the loop, counter: {}".format(counter))
|
||||||
```
|
```
|
||||||
|
<details>
|
||||||
|
<summary>Another example</summary>
|
||||||
|
|
||||||
|
```python3
|
||||||
|
FirstValue = 2
|
||||||
|
SecondValue = 0
|
||||||
|
while FirstValue>SecondValue:
|
||||||
|
print("The second value is bigger")
|
||||||
|
SecondValue = SecondValue + 1
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
Two *extra* things might look new to you here.
|
Two *extra* things might look new to you here.
|
||||||
First the `import time` and `time.sleep(1)`, can you tell me what it does?
|
First the `import time` and `time.sleep(1)`, can you tell me what it does?
|
||||||
|
@ -1373,14 +1385,14 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
There is three types of logical operators. All operators returns boolean.
|
There is three types of logical operators. All operators returns boolean.
|
||||||
|
|
||||||
| Operator | Result |
|
| Operator | Result |
|
||||||
|-----------------|----------------------------------------------|
|
|-----------------|---------------------------------------------|
|
||||||
| **And** | It send 'True' if all conditions are true |
|
| **And** | It send `True` if all conditions are true |
|
||||||
| **Or** | It send 'True' if one of conditions are true |
|
| **Or** | It send `True` if one of conditions are true |
|
||||||
| **Not** | It reverse the boolean result |
|
| **Not** | It reverse the boolean result |
|
||||||
|
|
||||||
|
|
||||||
Let's start example with 'And' operator!
|
Let's start example with `And` operator !
|
||||||
|
|
||||||
```python3
|
```python3
|
||||||
CustomerName = "Jean"
|
CustomerName = "Jean"
|
||||||
|
@ -1394,9 +1406,51 @@ if CustomerAgreement and DealerAgreement :
|
||||||
else:
|
else:
|
||||||
print(f"Oh no {CustomerName} and {DealerName} are disagreed ;( ")
|
print(f"Oh no {CustomerName} and {DealerName} are disagreed ;( ")
|
||||||
```
|
```
|
||||||
As you can guess, Jean and Paul are agreeing to the deal. If I had put 'False' in DealerAgreement, the result will be inverse.
|
As you can guess, Jean and Paul are agreeing to the deal. If I had put 'False' in DealerAgreement boolean, the result will be inverse.
|
||||||
|
|
||||||
|
Let's show an another example with the `Or` operator.
|
||||||
|
|
||||||
|
````python3
|
||||||
|
def Choice_cold_hot(Temperature):
|
||||||
|
if Temperature <= 20 or Temperature >= 40:
|
||||||
|
print("Don't go outside")
|
||||||
|
else:
|
||||||
|
print("Let's go to the beach")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
Temperature = int(input("What is the temperature"))
|
||||||
|
Choice_cold_hot(Temperature)
|
||||||
|
````
|
||||||
|
Look at this code, if the temperature is smaller than 20° or bigger than 40°, you must stay home and don't go to the beach. So, if I put 35° for temperature, it will say that you should go to the beach.
|
||||||
|
|
||||||
|
Let's make an exercise. You have to take the previous code and use the `And` operator.
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Spoiler warning</summary>
|
||||||
|
|
||||||
|
````python3
|
||||||
|
def Choice_cold_hot(Temperature):
|
||||||
|
if Temperature >= 20 and Temperature <= 40:
|
||||||
|
print("Let's go to the beach")
|
||||||
|
else:
|
||||||
|
print("Don't go outside")
|
||||||
|
if __name__ == "__main__":
|
||||||
|
Temperature = int(input("What is the temperature"))
|
||||||
|
Choice_cold_hot(Temperature)
|
||||||
|
````
|
||||||
|
</details>
|
||||||
|
|
||||||
|
Now, we have used that operators, we can use the last logical operator. The `Not` operator sends the reverse of the result.
|
||||||
|
|
||||||
|
````python3
|
||||||
|
if __name__ == "__main__":
|
||||||
|
Gas = input("Do you want some gas")
|
||||||
|
if not Gas.startswith("y"):
|
||||||
|
print("Ok, no problem")
|
||||||
|
else:
|
||||||
|
print("That will be expensive")
|
||||||
|
````
|
||||||
|
In this example, if you tap yes, the result will be reversed.
|
||||||
# Lists
|
# Lists
|
||||||
|
|
||||||
The different built-in objects we've seen until now, such as `str` and `int` are simple [text](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str) and [numeric](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex) types.
|
The different built-in objects we've seen until now, such as `str` and `int` are simple [text](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str) and [numeric](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex) types.
|
||||||
|
@ -1537,6 +1591,16 @@ print("{} closes the door behind him...".format(friend.capitalize()))
|
||||||
|
|
||||||
**TODO pizza function with multiple arguments**
|
**TODO pizza function with multiple arguments**
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Simpler example</summary>
|
||||||
|
|
||||||
|
````python3
|
||||||
|
for i in range(5):
|
||||||
|
print(i)
|
||||||
|
````
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
# Coding challenge - Cheerleader chant
|
# Coding challenge - Cheerleader chant
|
||||||
|
|
||||||
Can you make me a program that outputs this type of cheerleader chant?
|
Can you make me a program that outputs this type of cheerleader chant?
|
||||||
|
@ -2630,101 +2694,118 @@ TODO add a countdown timer to the multiple choice game
|
||||||
|
|
||||||
TODO event loop introduction
|
TODO event loop introduction
|
||||||
|
|
||||||
## wxpython helloworld
|
## Tkinter helloworld
|
||||||
|
|
||||||
The absolute most basic way to have a *hello world* GUI program up and running with wxpython is the following.
|
The absolute most basic way to have a *hello world* GUI program up and running with Tkinter is the following.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import wx
|
from tkinter import *
|
||||||
|
|
||||||
app = wx.App()
|
root = Tk()
|
||||||
|
|
||||||
frame = wx.Frame(None, title="hello world")
|
MyLabel = Label(root,text="hello world")
|
||||||
|
MyLabel.pack()
|
||||||
|
|
||||||
frame.Show()
|
root.mainloop()
|
||||||
|
|
||||||
app.MainLoop()
|
|
||||||
```
|
```
|
||||||
|
Tkinter have two popular architectures, the Tcl and Tk. This two architectures are different, they have their own functionality and their own official documentation.
|
||||||
|
We are gonna use the Tk architecture.
|
||||||
|
|
||||||
|
The Tk architecture is used to create a GUI widget. He adds a lot of custom commands, so the widget is very customizable.
|
||||||
|
|
||||||
|
In the previous code,the `mainloop()` instruction allows us to open the main window and to not close immediately the window.
|
||||||
|
And then, the `Label()` method creates label in a layout. This method has many parameters to customize a label. The instruction `pack()` will be always call , this instruction can
|
||||||
|
add and adjust a widget.
|
||||||
|
|
||||||
While it works we know better by now.
|
While it works we know better by now.
|
||||||
We should include the `if __name__ == "__main__"` statement.
|
We should include the `if __name__ == "__main__"` statement.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import wx
|
from tkinter import *
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = wx.App()
|
root = Tk()
|
||||||
frame = wx.Frame(None, title="hello world")
|
|
||||||
frame.Show()
|
MyLabel = Label(root,text="hello world")
|
||||||
app.MainLoop()
|
MyLabel.pack()
|
||||||
|
|
||||||
|
root.mainloop()
|
||||||
```
|
```
|
||||||
|
|
||||||
The instance of `wx.App` is what will actually process our event loop and the `wx.Frame` is the content of our window.
|
The instance of `Tk()` is what will actually process our event loop and the `root` is the content of our window. We can customize `root` with instruction like geometry, title,etc.
|
||||||
In the latter we will create our button and labels so should create our own class and inherit from it.
|
In the latter we will create our button and labels so should create our own class and inherit from it.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import wx
|
from tkinter import *
|
||||||
|
|
||||||
|
class MainWindow(Frame):
|
||||||
class MainWindow(wx.Frame):
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
wx.Frame.__init__(self, None, title="hello world")
|
Frame.__init__(self,master=None)
|
||||||
self.Show()
|
Label.__init__(self, text="hello world")
|
||||||
|
self.pack()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = wx.App()
|
root = Tk()
|
||||||
|
root.title("title of the window")
|
||||||
|
root.geometry("500x300")
|
||||||
win = MainWindow()
|
win = MainWindow()
|
||||||
app.MainLoop()
|
root.mainloop()
|
||||||
```
|
```
|
||||||
|
|
||||||
We can add content to the *frame*, such as labels, input boxes and buttons as follows.
|
We can add content to the *frame*, such as labels, input boxes and buttons as follows.
|
||||||
Note the first argument to the `wx.StaticText` creation.
|
|
||||||
This is *where* we put the label *into*.
|
|
||||||
It kind of works but we'll encounter a **problem** when we pack more visual **objects** into the same frame.
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import wx
|
from tkinter import *
|
||||||
|
|
||||||
|
class MainWindow(Frame):
|
||||||
class MainWindow(wx.Frame):
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
wx.Frame.__init__(self, None, title="hello world")
|
Frame.__init__(self,master=None)
|
||||||
self.label = wx.StaticText(self, label="this is a label")
|
Label.__init__(self, text="hello world")
|
||||||
self.Show()
|
#Label
|
||||||
|
MyLabel = Label(self, text="This is a label")
|
||||||
|
MyLabel.pack()
|
||||||
|
|
||||||
|
self.config(bg="yellow")
|
||||||
|
self.pack()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = wx.App()
|
root = Tk()
|
||||||
|
root.title("title of the window")
|
||||||
|
root.geometry("500x300")
|
||||||
win = MainWindow()
|
win = MainWindow()
|
||||||
app.MainLoop()
|
root.mainloop()
|
||||||
```
|
```
|
||||||
|
|
||||||
Let's try to put multiple visual object into the same frame.
|
Let's try to put multiple visual object into the same frame.
|
||||||
|
|
||||||
```python
|
```python3
|
||||||
import wx
|
from tkinter import *
|
||||||
|
|
||||||
|
class MainWindow(Frame):
|
||||||
class MainWindow(wx.Frame):
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
wx.Frame.__init__(self, None, title="hello world")
|
Frame.__init__(self,master=None)
|
||||||
self.label = wx.StaticText(self, label="this is a label")
|
Label.__init__(self, text="hello world")
|
||||||
self.input = wx.TextCtrl(self)
|
#Label
|
||||||
self.button = wx.Button(self)
|
MyLabel = Label(self, text="This is a label")
|
||||||
self.Show()
|
MyLabel.pack()
|
||||||
|
#Button
|
||||||
|
MyButton = Button(self, text="I'm clickable!")
|
||||||
|
MyButton.pack()
|
||||||
|
|
||||||
|
self.config(bg="yellow")
|
||||||
|
self.pack()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = wx.App()
|
root = Tk()
|
||||||
|
root.title("title of the window")
|
||||||
|
root.geometry("500x300")
|
||||||
win = MainWindow()
|
win = MainWindow()
|
||||||
app.MainLoop()
|
root.mainloop()
|
||||||
```
|
```
|
||||||
|
|
||||||
You see how they are *stacked* one on top of the other?
|
You see how they are *stacked* one on top of the other?
|
||||||
We can overcome this problem with [sizers](https://wxpython.org/Phoenix/docs/html/sizers_overview.html).
|
We can overcome this problem with parameters of [pack()](https://wxpython.org/Phoenix/docs/html/sizers_overview.html).
|
||||||
There are multiple ones we can use but let's dive into a first one called a **box sizer**.
|
We can also use other geometry managers like [grid()](https://www.pythontutorial.net/tkinter/tkinter-grid/) and [place()](https://www.pythontutorial.net/tkinter/tkinter-place/).
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import wx
|
import wx
|
||||||
|
|
Loading…
Reference in New Issue