Benutzer-Werkzeuge

Webseiten-Werkzeuge


en:examples

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen RevisionVorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
en:examples [2022/02/01 10:14] – [Linked List] roehneren:examples [2022/08/25 21:28] (aktuell) – [EAN check] roehner
Zeile 1: Zeile 1:
-===== Exampes =====+===== Examples =====
 ==== EAN check ==== ==== EAN check ====
 +=== Tkinter/TTK ===
  
-The EAN check is an example of a simple GUI application. It has an entry component for entering an EAN and a text component for the multi-line output of results.+The EAN check is an example of a simple GUI application. It has an entry widget for entering an EAN and a text widget for the multi-line output of results.
  
 {{en:eancheck.png}} {{en:eancheck.png}}
Zeile 11: Zeile 12:
 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('An EAN must not contain any characters from digits!')+        self.output('An EAN must not contain any characters other than digits!')
     if len(EAN) == 13:     if len(EAN) == 13:
         self.output('Length of EAN: 13 ')         self.output('Length of EAN: 13 ')
Zeile 42: Zeile 43:
 </code> </code>
  
-According to the IPO principle, the input from the entry component is read in first. The text variable EANTV assigned to the entry component is used for input or output. Its value can be read with ENATV.get() for input and with EAN.set(value) for output. With+According to the IPO principle, the input from the entry widget is read in first. The control variable eEANCV assigned to the entry widget is used for input or output. Its value can be read with eENACV.get() for input and with eEANCV.set(value) for output. With
  
 <code python> <code python>
-    EAN = self.EANTV.get()+    EAN = self.eEANCV.get()
 </code> </code>
-the EAN entered in the Entry component is read into the local variable EAN of type **str(ing)** and can then be processed. +the EAN entered in the Entry widget is read into the local variable EAN of type **str(ing)** and can then be processed. 
  
 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 //Output//. It has very powerful methods, so even just outputting a line isn't quite easy. A separate method //output()// is therefore used in order to simplify programming.+Outputs are made to the text widget //Output//. It has very powerful methods, so even just outputting a single line isn't quite easy. A separate method //output()// is therefore used in order to simplify programming.
  
 <code python> <code python>
Zeile 57: Zeile 58:
     self.Output.insert('end', line + '\n')     self.Output.insert('end', line + '\n')
 </code> </code>
-This inserts a line at the end of the text component //Output// and ends it with the control character '\n' (NewLine). If an output line consists of several substrings, these must be put together with "+". Numbers must first be converted into a string using **str**(). +This inserts a line at the end of the text widget //Output// and ends it with the control character '\n' (NewLine). If an output line consists of several substrings, these must be put together with "+". Numbers must first be converted into a string using **str**().
  
 **Download**: **Download**:
   *[[https://www.guipy.de/examples/en/ean.zip|ean.zip]]    *[[https://www.guipy.de/examples/en/ean.zip|ean.zip]] 
 +
 +=== Qt variant of EAN check ===
 +In Qt we have a LineEdit widget for input and a PlainTextWidget for output. To read the EAN we use the //text()// method of LineEdit widget.
 +
 +<code python>
 +    EAN = self.leEAN.text()
 +</code>
 +
 +The output is easier in Qt than in Tkinter/TTK:
 +
 +<code python>
 +    def output(self, line):
 +        self.Output.appendPlainText(line)
 +</code>
 +  
 +**Download**:
 +  *[[https://www.guipy.de/examples/en/qtean.zip|qtean.zip]] 
  
 ==== Car==== ==== Car====
Zeile 72: Zeile 89:
 {{en:intro.png}} {{en:intro.png}}
  
-In the next step, the //car// class is used as a functional spezification in a GUI program.+In the next step, the //car// class is used as a functional specification in a GUI program.
  
 {{en:car.png}} {{en:car.png}}
Zeile 84: Zeile 101:
 </code> </code>
  
-When refueling, the quantity entered in the entry component is read in via the get() method of the text variable //eRefuelTV// belonging to the component and converted into the required data type using //float//.+=== Tkinter/TKK === 
 + 
 +When refueling, the quantity entered in the entry widget is read in via the get() method of the control variable //eAmountCV// belonging to the widget and converted into the required data type using //float//.
  
 <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 115:
 </code> </code>
  
-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 get_tankcontent() and get_mileage() of the class car and passed to the set() methods of the text variables eTankcontentTV and eMileageTV:+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 set() methods of the control variables eLicenseplateCV, eTankcontentCV and eMileageCV:
  
 <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(), y = 160)         self.lCar.place(x = self.car1.get_mileage(), y = 160)
 </code> </code>
  
-The label component //lCar// has been assigned a car image via the //Image// attribute in the object inspector. The place() method sets the x position of the car to the mileage.+The label widget //lCar// has been assigned a car image via the //Image// attribute in the object inspector. The //place()// method sets the x position of the car to the mileage.
  
 **Download** **Download**
   *[[https://www.guipy.de/examples/en/car.zip |car.zip]]    *[[https://www.guipy.de/examples/en/car.zip |car.zip]] 
 +
 +=== 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()
 +</code>
 +
 +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 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, leTankcontent and leMileage:
 +
 +<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(), y = 160)
 +</code>
 +
 +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://www.guipy.de/examples/en/qtcar.zip |qtcar.zip]] 
  
 ==== Linked List ==== ==== Linked List ====
-Die Programmierung dynamische Datenstrukturen stellt eine erhebliche Herausforderung dardenn man muss das Konzept der Verkettung mittels Verweisen verstehen und komplexe Operationen mit Verweisen druchführenNeu dabei sind Variablen die Werte haben, welche Verweise auf Objekte bzw. Adressen sind+Programming dynamic data structures is a significant challengeas one must understand the concept of linking using references and perform complex operations on referencesNew are variables that have values that are references to objects or addresses.
  
-In diesem Beispiel betrachten wir eine einfach verkettete lineare ListeDie Liste selbst wird als Klasse //VerketteteListe// modelliert mit Knoten als ListenelementenAls Attribut hat sie einen Verweis auf den //Anfang// der Liste.+In this example we consider a singly linked linear listThe list itself is modeled as a class //LinkedList// with nodes as list elementsAs 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 //Nächster// den Verweis auf einen nächsten Knoten der verketteten ListeHat ein Knoten keinen nächsten Knotenso erhält dieses Attribut den Wert //None//.+The //Node// class can store data in the first attribute and a reference to a next node of the linked list in the //next// attributeIf a node has no next nodethis attribute has the value //None//.
  
 <code python> <code python>
Zeile 126: Zeile 177:
 </code> </code>
  
-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 //einfügen_AmAnfang()// implementieren:+The first two cases can be implemented with the method //insert_first()//:
  
 <code python> <code python>
Zeile 140: Zeile 191:
 </code> </code>
  
-Im UML-Fenster lässt sich die Implementierung testenDazu erzeugt man eine verkettete Liste und einen Knotenruft über das Objekt //verketteteliste1// die Methode //einfügen_AmAnfang()// auf und gibt für den Parameter einKnoten den Wert //knoten1// an.+The implementation can be tested in the UML windowTo do thisyou 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 durchhangelnDort 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 elementThe new node is added there.
  
 <code python> <code python>
Zeile 157: Zeile 208:
 </code> </code>
  
-Am schwierigsten ist das Einfügen vor einem Knoten, denn der neue Knoten muss dann hinter dem vorhergehenden Knoten eingefügt werdenNeben dem Cursor braucht man dazu einen weiteren Verweise auf den vorherigen KnotenDer 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 nodeIn addition to the cursor, you need another reference to the previous nodeThe node before which to insert is determined by its data.
  
 <code python> <code python>
Zeile 177: Zeile 228:
 </code> </code>
  
-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 244:
 </code> </code>
  
-Durch interaktives Testen im UML-Fenster kann man die Funktionsfähigkeit der implementierten Methoden sehr gut prüfendenn das Ergebnis wird unmittelbar im Objektdiagramm visualisiertMan erkennt sofort, ob eine Methode wie gedacht funktioniert hatIm Bild wurde zum Testen der //löschen()//-Methode die Liste mit den Elementen 'a, 'b' und erzeugt und dann der Knoten mit Daten = 'b' gelöschtDas hat funktioniertdenn die Liste besteht nur noch aus den beiden Elementen 'a' und 3.+The functionality of the implemented methods can be checked very well by interactive testing in the UML windowbecause the result is visualized in the object diagramYou can see immediately whether a method worked as expectedIn the picture the list with the elements 'a, 'b' and was created and then the node with data = 'b' was deletedThat workedbecause the list consists only of the two elements 'a' and 3. The deleted node is just deleted from the list. Python's garbage collection takes care of it. 
 + 
 +{{en:list.png}} 
 + 
 +**Important NOTE** 
 + 
 +In the UML window, objects are identified by their addresses. In the example, the address 0x01CFA688 for the node1 object. 
 + 
 +<code python> 
 +>>> node1 = Node('a'
 +>>> print(node1) 
 +<__main__.Node object at 0x01CFA688> 
 +</code>
  
-{{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://www.guipy.de/examples/de/liste.zip |liste.zip]] +  *[[https://www.guipy.de/examples/en/list.zip |list.zip]] 
  
  
en/examples.1643706865.txt.gz · Zuletzt geändert: 2022/02/01 10:14 von roehner