en:examples
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende ÜberarbeitungLetzte ÜberarbeitungBeide Seiten der Revision | ||
en:examples [2022/02/01 10:14] – [Linked List] roehner | en:examples [2022/08/22 11:07] – [Car] roehner | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | ===== Exampes | + | ===== Examples |
==== EAN check ==== | ==== EAN check ==== | ||
- | The EAN check is an example of a simple GUI application. It has an entry component | + | The EAN check is an example of a simple GUI application. It has an entry widget |
{{en: | {{en: | ||
Zeile 11: | Zeile 11: | ||
def bCheck_Command(self): | def bCheck_Command(self): | ||
# Input | # Input | ||
- | EAN = self.EANTV.get() | + | EAN = self.eEANCV.get() |
# Processing | # Processing | ||
if not EAN.isdigit(): | if not EAN.isdigit(): | ||
- | self.output(' | + | self.output(' |
if len(EAN) == 13: | if len(EAN) == 13: | ||
self.output(' | self.output(' | ||
Zeile 42: | Zeile 42: | ||
</ | </ | ||
- | According to the IPO principle, the input from the entry component | + | According to the IPO principle, the input from the entry widget |
<code python> | <code python> | ||
- | EAN = self.EANTV.get() | + | EAN = self.eEANCV.get() |
</ | </ | ||
- | the EAN entered in the Entry component | + | the EAN entered in the Entry widget |
The processing part checks whether 13 digits have been entered and then performs the check digit calculation. | The processing part checks whether 13 digits have been entered and then performs the check digit calculation. | ||
- | Outputs are made to the text component labeled | + | Outputs are made to the text widget |
<code python> | <code python> | ||
Zeile 57: | Zeile 57: | ||
self.Output.insert(' | self.Output.insert(' | ||
</ | </ | ||
- | This inserts a line at the end of the text component | + | This inserts a line at the end of the text widget |
**Download**: | **Download**: | ||
Zeile 72: | Zeile 71: | ||
{{en: | {{en: | ||
- | In the next step, the //car// class is used as a functional | + | In the next step, the //car// class is used as a functional |
{{en: | {{en: | ||
Zeile 84: | Zeile 83: | ||
</ | </ | ||
- | When refueling, the quantity entered in the entry component | + | === Tkinter/TKK === |
+ | |||
+ | When refueling, the quantity entered in the entry widget | ||
<code python> | <code python> | ||
def bRefuel_Command(self): | def bRefuel_Command(self): | ||
# Input from the GUI | # Input from the GUI | ||
- | amount = float(self.eRefuelTV.get()) | + | amount = float(self.eAmountCV.get()) |
# Processing | # Processing | ||
self.car1.refuel(amount) | self.car1.refuel(amount) | ||
Zeile 96: | Zeile 97: | ||
</ | </ | ||
- | Then, according to the IPO principle, the entered amount is processed in the refuel() method. | + | Then, according to the IPO principle, the entered amount is processed in the //refuel()// method. |
- | Finally, the result is output with a separate method show(). The values for tank contents and mileage are retrieved via the get() methods | + | Finally, the result is output with a separate method |
<code python> | <code python> | ||
def show(self): | def show(self): | ||
- | self.eTankcontentTV.set(self.car1.get_tankcontent()) | + | self.eLicenseplateCV.set(self.car1.get_licenseplate()) |
- | self.eMileageTV.set(self.car1.get_mileage()) | + | self.eTankcontentCV.set(self.car1.get_tankcontent()) |
+ | self.eMileageCV.set(self.car1.get_mileage()) | ||
self.lCar.place(x = self.car1.get_mileage(), | self.lCar.place(x = self.car1.get_mileage(), | ||
</ | </ | ||
- | The label component | + | The label widget |
**Download** | **Download** | ||
*[[https:// | *[[https:// | ||
+ | |||
+ | === Qt === | ||
+ | |||
+ | When refueling, the quantity is read in via the text() method of the LineEdit widget and converted into the required data type using //float//. | ||
+ | |||
+ | <code python> | ||
+ | def bRefuel_Command(self): | ||
+ | # Input from the GUI | ||
+ | amount = float(self.leAmount.text()) | ||
+ | # Processing | ||
+ | self.car1.refuel(amount) | ||
+ | # Output | ||
+ | self.show() | ||
+ | </ | ||
+ | |||
+ | Then, according to the IPO principle, the entered amount is processed in the // | ||
+ | |||
+ | Finally, the result is output with a separate method //show()//. The values for license plate, tank contents and mileage are retrieved via the get() methods of the class car and passed to the setText() methods of the widgets leLicenseplate, | ||
+ | |||
+ | <code python> | ||
+ | def show(self): | ||
+ | self.leLicenseplate.setText(self.car1.get_licenseplate()) | ||
+ | self.leTankcontent.setText(self.car1.get_tankcontent()) | ||
+ | self.leMileage.setText(self.car1.get_mileage()) | ||
+ | self.lCar.move(x = self.car1.get_mileage(), | ||
+ | </ | ||
+ | |||
+ | The label widget //lCar// has been assigned a car image via the //Pixmap// attribute in the object inspector. The //move()// method sets the x position of the car to the mileage. | ||
+ | |||
+ | **Download** | ||
+ | *[[https:// | ||
==== Linked List ==== | ==== Linked List ==== | ||
- | Die Programmierung dynamische Datenstrukturen stellt eine erhebliche Herausforderung dar, denn man muss das Konzept der Verkettung mittels Verweisen verstehen und komplexe Operationen mit Verweisen druchführen. Neu dabei sind Variablen die Werte haben, welche Verweise auf Objekte bzw. Adressen sind. | + | Programming dynamic data structures is a significant challenge, as one must understand the concept of linking using references and perform complex operations on references. New are variables that have values that are references to objects or addresses. |
- | In diesem Beispiel betrachten wir eine einfach verkettete lineare Liste. Die Liste selbst wird als Klasse | + | In this example we consider a singly linked linear list. The list itself is modeled as a class //LinkedList// with nodes as list elements. As an attribute it has a reference to the //head// of the list. |
- | Die Klasse Knoten kann im ersten Attribut Daten speichern kann und im Attribut | + | The //Node// class can store data in the first attribute and a reference to a next node of the linked list in the //next// attribute. If a node has no next node, this attribute has the value //None//. |
<code python> | <code python> | ||
Zeile 126: | Zeile 159: | ||
</ | </ | ||
- | Die Implementierung einer Einfügeoperation ist schwierig, weil vier Fälle zu unterscheiden sind: | + | The implementation of an insert operation is difficult because four cases must be distinguished: |
- | - Die Liste ist leer. | + | - The list is empty. |
- | - Es soll am Anfang eingefügt werden. | + | - It should be inserted at the beginning. |
- | - Es soll am Ende eingefügt werdenn. | + | - It should be inserted at the end. |
- | - Es soll zwischen zwei Knoten eingefügt werden. | + | - It is intended to be inserted between two nodes. |
- | Die beiden ersten Fälle lassen sich mit der Methode | + | The first two cases can be implemented with the method |
<code python> | <code python> | ||
Zeile 140: | Zeile 173: | ||
</ | </ | ||
- | Im UML-Fenster lässt sich die Implementierung testen. Dazu erzeugt man eine verkettete Liste und einen Knoten, ruft über das Objekt | + | The implementation can be tested in the UML window. To do this, you create a linked list and a node, call the //insert_first()// method via the //linkedlist1// object and enter the value //node1// for the node parameter. |
- | {{de:liste1.png}} | + | {{en:list1.png}} |
- | Zum Einfügen am Ende der Liste muss man sich mit einer Schleife und einer Cursor-Variablen zum letzten Element durchhangeln. Dort wird der neue Knoten angefügt. | + | To insert at the end of the list, you must use a loop and a cursor variable to navigate to the last element. The new node is added there. |
<code python> | <code python> | ||
Zeile 157: | Zeile 190: | ||
</ | </ | ||
- | Am schwierigsten ist das Einfügen vor einem Knoten, denn der neue Knoten muss dann hinter dem vorhergehenden Knoten eingefügt werden. Neben dem Cursor braucht man dazu einen weiteren Verweise auf den vorherigen Knoten. Der Knoten vor dem einfügt werden soll wird durch seine Daten bestimmt. | + | Inserting before a node is the most difficult case because the new node must then be inserted after the previous node. In addition to the cursor, you need another reference to the previous node. The node before which to insert is determined by its data. |
<code python> | <code python> | ||
Zeile 177: | Zeile 210: | ||
</ | </ | ||
- | Vergleichbar schwierig ist das Löschen eines Knotens, denn zum Löschen muss der vorherige Knoten mit dem nachfolgenden verbunden werden. | + | Deleting a node is just as difficult because the previous node must be connected to the following one in order to delete it. |
<code python> | <code python> | ||
Zeile 193: | Zeile 226: | ||
</ | </ | ||
- | Durch interaktives Testen im UML-Fenster kann man die Funktionsfähigkeit der implementierten Methoden sehr gut prüfen, denn das Ergebnis wird unmittelbar im Objektdiagramm visualisiert. Man erkennt sofort, ob eine Methode wie gedacht funktioniert hat. Im Bild wurde zum Testen der // | + | The functionality of the implemented methods can be checked very well by interactive testing in the UML window, because the result is visualized in the object diagram. You can see immediately whether a method worked as expected. In the picture the list with the elements |
+ | |||
+ | {{en: | ||
+ | |||
+ | **Important NOTE** | ||
+ | |||
+ | In the UML window, objects are identified by their addresses. In the example, the address 0x01CFA688 for the node1 object. | ||
+ | |||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | < | ||
+ | </ | ||
- | {{de:liste.png}} | + | Therefore, do not implement a %%__repr__%% function for your classes, because then these addresses will no longer be available. |
**Download**: | **Download**: | ||
- | *[[https:// | + | *[[https:// |
en/examples.txt · Zuletzt geändert: 2022/08/25 21:28 von roehner