 Welcome everybody. In this talk, we will see the Python QT or PyQT QT depends of the accent, the pronouns, but yeah. This API for interfaces in with Python. So let's start to talk. And I'm Danielis Cacafrate from Italy. I'm here with the Italian Acre Embassy. And I have our budget room, but we work mainly with WordPress stuff. But yeah, I'm also a Mozilla blogger. I've done the doc before about WordPress developer tools. But I'm involved in too many things. And that's for work. We are starting right now to use Internet of Things for stuff. But in the past year, already developed with Python QT. Now we will see what we can do very, very quickly and very simple. So when I started to use Linux like few years ago, switched from Windows using .NET, these horrible stuff also today are horrible. But when I switched, I was doing high school and I was developing application softwares. So when I arrived to Linux, it's amazing. You can do everything you want. The problem is that you need to know what you want to do and how because you can break everything. So this was me when I started Linux. I was 19 when I switched to Linux as daily, now everywhere. But this was me. So when I want to do the same things on Windows, I want to do software with interface also Linux. And at the time, the time was very difficult and not so much simple like with Visual Basic and this kind of tool. Now it's different. But at the time it was very difficult to start because the documentation interface, we know like 10 years ago the interface on Linux was like difficult or horrible and now it's completely different. But the moment was, I have no idea how can I do what I want to do. So I chose to learn Python only because I was using KDA, like right now, I love KDA. And I usually buy a book, read all the books and start to develop. So there was only this book about QT in Python because I'm not C++ developer like right now after this year because I'm web developer. So I discovered Python using this book. And it's the only book like also today. But it's very, very interesting because the PI of QT is not changed so much but the fundamental are the same. And it was a huge book. And it was interesting because when I saw Python I said no, for me it's too much because from Visual Basic and after years of PHP or Java I said no, there are no braces, there is the indentation, oh my God, it's possible to, I hate Python. After years I continued to dislike Python for that but I think it's very, very powerful and very easy to do. You need only to accept that. But it will improve your code quality very quickly. So my choice of Python was only because I love QT, I want to do something with that. And it's strange because someone chose a language because it's new, it's amazing, etc. I chose only for interfaces, it's very strange. And Python, I try to explain, I can explain Python to a public of Acre. I think everywhere in here use Python so it was not easy. So I use the memory way sometimes works, sometimes. And I said, okay, Acres want to see code, want to see a workshop demo, want to see inaction. So this is my portfolio of QT interface public, we can say that way, of stuff. And now today we will see the boilerplate but I've done a few interfaces with PyQT in the time, with four. And when I chose to do this talk I never used QT5 so I say, oh my God, I have to, I applied for a talk without know what to do again. So I started a bit, I see that all my old code works also with QT5. So it was a little surprise. And the good part of QT is that usually it's very, very easy to start, especially in Python because there is no compiler stuff, etc. So it's more fast, of course. And so I chose to do screen right now. This is QT designer. The results on GTK, this is Glade, I don't remember the name. And when I saw this, when I switch from Visual Studio at the time, I say, okay, it's like Visual Studio. So I know what to do. And it's more easy, I think, of Visual Studio. These are three interfaces of my old tool in QT4, in QT Creator 4 or 5, because the cool part of QT that you can have right now with QT5, you have different ways to do an interface. Usually in QT you have three, now you have three ways. You can do the interface and save a UI file that is an XML file. So it's a standard. So you can have the same interface during the year that works because it's the same file. And this is very important because, as you can see, after a year, the interface is always rendered the same way because it's a standard. And this is important, very, very important, because I can imagine what it means to work in legacy stuff that was ever. So you can do a new UI file, like this XML file. And when you're developing with every language, it's important to have autocomplete other stuff. So the cool part that you have a file in Python, in that case, can be compiled as code, because QT, of course, interprets elaborate parts. The XML file generates something inside. But we can convert this XML file as Python code. In that way, our editor can use this code as base for autocomplete syntax alighting. So it's more quickly to do. So usually my workflow is to create an interface right here. This is QT Designer on Debian Seed. It's here. You can install, open UI file. Do your interface, because it's important for me, always do the interface and next do the code. Because while I develop the interface, I say, OK, I need this feature, this, this, this. Save the file, because once I define the file, I don't want to edit again the interface. So I can compile as Python code. So as you can see, there is the command line that is the same for QT4 and QT5. So it's the same command change the value, the number. And you can compile as Python code, poor Python code. As I can say, better integration with editor, because actually QT Creator can save Python file, but it's not autocompleting all the stuff. So you have to use other tool to develop with PyQT. So it's better to have Python code. Of course, it's better to read, respect XML file, because it can be verbose, long file, etc. Python code is more easy to read and understand what is happening. Because the second way is to write directly the Python code, instead use QT Creator. But we are developers, we are hackers, but sometimes an interface, also to create interface, is more quickly fast. So I suggest to use this way, because you can do whatever you want. Right now with QT5, there is QML. There is another language that I don't wish to show this talk, because I think it's too much to see for an introduction time, because what is the difference between QT and QML? QT uses its C++ code interpreted, blah, blah, blah, and generate interface. QML is running with OpenGL. So the interface is the same. You can use the same interface running under OpenGL. So it's more performance. And it's another standard, it's JSON-based, and then can have inline JavaScript code. So it's completely different, but it's more performance. So right now with QML, you can do more cool things. But it's another step. It's better to start with a simple one, because usually, as we show, it's better to have a little UI for what you need. So it's time to see where to start. For my development, I created my simple, very simple boilerplate for development. It's on GitHub. I have to try to open it now. And I've done for this talk two versions, the QT4 version and the QT5 version. It's the same. I've only changed the import, so it's very, very easy. And the cool part of compiling that for PyQT4, there is an interface, where you can do the folder and automatically convert the file for PyQT5. I've done the same also, a batch script file, because it's more fast, but yeah, we can do it also this. So I open a PyQT5 file. I use a Python editor written with QT. It's an exception, but the part of this work. So this is, of course, PyQT works Python 2 and Python 3. There are differences, of course, because we know the cool part of Python 3 is the support of Unicode. But QT is a big framework and support Unicode. So the difference between Python 2 and Python 3 that, natively, in Python 3, we use the Python Unicode system. So we use the native Python stuff for Unicode. With Python 2, we use the QT, a framework stuff for Unicode. So this is the main difference between the 3 and true version with PyQT4 and 5. This is the most problem. For that reason, I started to develop in poor Python 3 to avoid any confusion, because, of course, is a poor standard Python. So we can use also other libraries and frameworks without problems. So what we do? We have, of course, the PyQT library stuff. It's the basics, but it's better to remember. We import the QT core that is the core framework. We need to, don't forget that QT is not all interface. QT has process stuff, file watcher, and many other things. So I think it's not so much a framework, but a huge framework is the right term, because you can do it in poor QT, everything you need, as is so in the past years. So you can see that you don't need often other library. QT guy is the interface, of course, or the element of the interface. Widgets are the components of the interface that we can have here in the designer. These are widgets. And, of course, you can have widgets, custom widgets. We have here from KDE, but you can develop your own. Oh, wrong. Okay. So you need only these three lines to get working. A QT script, software, whatever. I add this one because our more useful right now, we will see why. When we develop, design the interface with QT creator, we get the file name. Usually the standard one is main window because it's the main window, of course. So as you can see, wrong desktop again. We have main window with all the element right here. And we can have here all the attributes of the element. One of the cool parts right here in this demo that I opened, the spatial, in that way, it supports the resides of the window without breaking everything. And we can change everything we want. So in Python, we will use this. This is the name of the element. Label 2, 2, 2, Spinbox 2, whatever. Okay. So we take this real file. We compile in Python to import. We'll do it in that way. Like as usual class in Python because this is poor Python code at the end. So we can create a class. We import the object. We import the new class of need. And we used one of the most used, I guess, feature of QT, settings class to save our settings. How works this? It depends on the system. Put the config file that automatically is created. So we doesn't care about what is the path because QT will do for us. In Linux, it's your own folder, Gilesh config, your name, and the name of the software. Dot ini. And you can also use different format file QT will do automatically for you. The ini is the default. That works. This is important. So in that way, automatically we create the QTQ settings class, object, as you want. And we init the class. We have developed, we already know this part. But we init the window. In that way, load everything. Load the UI. This command initialize the Y and we assign to an object. Yes. Oh, yeah. Okay. And we created the object. We initialize the object. We assign this object in the class because it's comfortable to have the class, of course. And set up the object in that way is assigned. We can, that way, we can write the title of the window. Very simple. This is, we will see now this what is. And we show the window. In that way, we show the window very quickly. Important, it's very easy. For initialize, I use this way, but, of course, there are many ways to initialize a class in Python. The cool part of Python that you can do the same things in too much way. So this is one of the way. We created a key application that way can intercept the parameter we can send from command line. And we initialize the window, set up exit. The exit is for close the window, but not to close the process. This is important to notize. Now we will see why. Because that way we close when click the cross of the window. Automatically it's closed with this command. But, we have an interface. We only show the interface and the code without talk with each other. This is the problem. Also with the web we can do an interface and we can add events or stuff. How we can do it in QT? There is a slot and signal way. And for me when I switched from windows was kind strange to understand how it works. Because the standard inside QT changed at different time. Now with QT5 there is another way more simple. But I prefer this one because for me it's the most simple. And I will say how it works. This is a slot and a signal in the same line. What we have? Self UI this is the window. Very simple. We have the button. New question button. Clicked is the action. So every widget has is action. So we don't have the best way to find is the QT documentation. That is automatically generated you can use also the C++ code. For me that came from web stuff. When I have to find something to do with PQT it's difficult to find the stack overflow or other project or tutorial whatever. But for C++ there is too much documentation tutorial. And you need only to convert the syntax for Python. So you can use the same snippet for C in Python without problem. And this is important because the documentation the API is the same between the languages. It's not changing. So we ask for the action click. We can have double blah blah blah. Every widget has is own signal. We connect to this and we leave a callback. Like with the web. We say that when you click you need to run self new question. This is a slot and signal. So. This is important because that way we can like Python is important because you can you are forced to write readable code because we don't have braces. So we can structure with classes object blah blah blah. So in that way we can create a function that we can use different times in different buttons whatever. We do not create application etc. Or we can ignore in that case the button that execute the action. So. The signal is the action that is happening and the slot is the callback. Pretty easy. We need only to remember this API and look at the documentation what are the actions of the widgets. For that reason it's better to develop the web. Excuse me. Design the interface because that way we can use all the widgets that we want and next we can take care of the action that we need because we can start to develop code without thinking the interface will be a mess. We will use loss so much time. So it's better to develop use the widgets that we want do the interface save compile around in Python. Okay. So we saw the signal and here we have a signal but it's not using a Qt's element widget class whatever. The problem that usually also for development we run script from the command line and when we create a Python script with Qt from command line when you use the control C to close the process it's not closing. Because there is no support in this Python script to react of this action on the command line. So this single line with signal say when there is the signal of interruption from command line close it the process. So in that way we can force the closing of the script from the command line and also we will see in the official example of Qt this line is missing so it cannot close from command line. But this helpful also for development or for people that run stuff with the scripts or whatever. So it's very important to have this line for that reason. This is mainly reason why you created the boilerplate. Remember this code. But also for the Qsetting part. Because Qsettings is like for local storage on the web. You have get and set value. So it's very quickly. So this is the boilerplate. As you can see I can create right now an interface put on the script that in the boilerplate and run. I will show an example with my one one. I don't remember if you have a PyQt interface No. But I think that I can use one of the example No. The cool part of life coding that you remember where there are the files. But I have I remember I have done I open another design file I save as main window. I have no idea where I save it, of course in the wrong folder. Now I have to compile the file. And I remember the scripts will work. Yep. I compiled the file as Python code. I opened this window as you can see is poor XML. Horrible to read but it's working and it's standard at the time. And we have here the Python code auto-compiled from the XML file with all the names so we can use that sort of code. Now we have the file with the name and if it works now I click here and it will start. Seems not. We will see now why. I have written wrong and can make case the name of the window. Python use this standard. The last word is uppercase the other one not. Again, there is something that is not working. I got it why. Wrong name of the file and of the objects. But you can change of course the name. This is my boilerplate. I don't remember what I do on the boilerplate. What? Okay, we can ignore this part. We will show running already an interface already compiled. Okay, this is the UI as you saw in the QT designer running. We have the UI file compiled in PyCode running. These are the official example of QT on GitHub but there are too many. I will show to you a few of them and this is interface. So and because QT support as you can see I cannot try to cover. Usually this is one of the problem of interface on desktop with QT you can use a widget to avoid this action. But you can support also resides of the element passed on the interface itself. So you can go bigger little. I only to configure on the QT designer compile and I don't. So it's like the end because I don't want to talk about so much about two hackers I want to show to you demo of QT can do for you. Because this is what you need to do to use QT. The only problem is have to have the idea to do an application with interface. Because the QT elements are very powerful you can do everything you want. So the example repo on PyCode on GitHub has few example I can show to you this is poor QT may need to do the action. We need really to understand this kind of things. And this code it's more the comment about the license that the code on Python I think. But as you can see we have only the here is designing the interface in poor Python. Reference to the file the enter event drop event mouse enter event around application. It's very very easy because the most boring part are by QT framework. You have to decide the interface what is the interface and how to interact with interface. But I think that there was one of the last feature of QT is the widgets of OpenGL that use the JavaScript interface. So you can do an OpenGL demo running in PyQT with a widget. I think right now there is the support in PyQT but I think they will be arrived very soon. But there was OpenGL this is the same running with different engine. I don't think there is so much different for the HD part but as you can see our fluid both but this is not running maybe I'm not running in the right way but the cool part that you can find these elements are pretty easy. One of the cool parts of QT that you can define in QT designer the hotkey and you can assign automatically with the slot. So you can do very quickly, assign the same method. Yes. Python is Python. So you can move the Python file, it's only a conversion. Yeah. I think I have never done because I use mainly Linux but as an example of this kind of stuff. The problem that I think QT on Windows is more difficult to install all the framework because QT are very heavy as a size. So I know there are different way to get only the library that you need also for Windows and not to install all the package. Yeah, of course. Yeah, yeah, I know I saw example for Python QT4 I saw also a tool that will automatically do for you. I don't know there is in the meantime also for 5 but there also stuff for Windows. So Windows is supported. And all these parts here are pretty easy to do. I've done list view with the checkbox in the list view only hacking with Python QT code in poor Python. Yes. I have already this part but you can also have there are people that run in PyQT also on Android. So you can also do it that way. But for Android I don't think that it's best so much good to have Python that use QT that use running in a Java system. Maybe it's better to use C++ QT that is poor compiler specifically for Android is better also for performance. Yeah. Yeah, you can do it in the same way we slot the signal. So all the media widgets as action and you give a callback also the same two different elements is automatically done. Yeah, you can do it also this you there are too many widgets and classes. I don't even show because I think that it's better way is to try to do and you can find documentation in my talk as all the lists of my application open source. There are links I can open few of them because there was issues with Wi-Fi yesterday I cannot do all of them when I prepare the talk. And this example is interface for Asana that we done in PyQT. As you can see this is poor PyQT there is the code here. There is a Python compiler the interface and the Python code using a library. We have different element and this is connected of course with Asana. Asana is a service to do list simple easy. In that way I can filter call the API and create interface with checklist automatically update interface the information settings open a new window of PyQT to put the APK code and this poor Python and you can also create a new task this is poor Python code but I think that this is the most interesting stuff that I done of it's all about works to continue to works. I hope they have a screen, yes because I don't remember the hot key when I started to develop this tool. It's like a quiz tool written in PyQT that asks you two questions do you remember what is the key try to guess what is the hot key but for me it was boring because developers for hot key want so shit shit so I created an interface to it's better to download and try it I hope that it works because the last time that I tried was a little while ago so what is the file their hot key library in any case I created an interface in poor PyQT they can create the XML file that I'm reading so automatically create on the fly new element of the interface and it's poor here just save an HTML file that open in a preview window, poor in PyQT and the code you can try it at home or right here but it's already here poor example this one is an interface for a less compiler this is a language that's compiled in CSS it's quite old but I know people that are using as you can these use a file watcher to see when there are new files in poor PyQT use the PyQT framework for file watcher so I don't use script or the stuff and run a process inside the computer and in the past I've done an interface I don't know its work again because it was a prototype and there are no screen I also show the output of the console inside the interface because it was a tool interface for Nmap and other tools never finished a print interface what is happening inside with the command so in poor PyQT you can do it all these kind of things I've done also a few years ago to promote PyQT in Italian but I want to show a few parts only to show the code of course no worry about the Italian and you can also do the classic message box in PyQT very easily to show alers is a line of code this came from the code of learn-auth case of course there is also a way to open a new window inside because the problem when you open a new window that you have to avoid that close the new window the application because there are like power and shield also can conflict so there is a little trick to avoid this kind of action you can open dialogue very easily for files with a filter because PyQT has a huge filter for these kind of things for the design interface I change the cursor for waiting a reply I put the loading cursor automatically with this line and put the back the old cursor this is the way I create a list of checkbox because usually no usually the native list view doesn't have the support for checkbox so you can create on the fly widget with a mix of other widgets I instead create this model of PyQT because PyQT have too many things to simplify I create a loop for every element I create add a checkbox I can add on the fly as you can see here a slot a signal to an element created on the fly so you can do like you do in the web pages you can do also an interface on desktop this is create a process inside QT framework so you don't need to use sub process like in Python and you have all the events right here so you can avoid to have different libraries so in that way the code will be always the same because the PI will be the same also you change the PyQT version my old version of PyQT 4 continue to work right now as which also PyQT 5 continue to work so it may I suggest is to use when you can use the QT framework stuff because works everywhere also after years this is a way to create autocomplete you have a method automatically open tooltip or suggest to you to write the text because there is also the syntax slider widget this is the watcher that you use in the learner autocase is a try waiting to open the second window right here in another file so you may suggest for a developer doesn't care doesn't afraid from documentation and code as I think it's here is full try to do with example available from PyQT 5 and see what happening but try also to develop an interface and tell to the first slot and signal because I started raining you can get fun very quickly only trying with demos and see that the face because I think that for developers they usually use command line it's difficult to think about an interface because it's not our job we do script for us but I think it's very important to try to design an interface and define what is happening when I click here and with Python QT is very simple like on the web because in the past I tried CT CTK and was not so much easy like that so I suggest to you to try the demo that there are very too many and I don't remember the demo because are too many there are not so the permission to run but this is an example of address book already available so the cool part of Python is very easy to read also don't worry other people so you can find everything you need probably also looking on this example or look on internet with C++ forums or whatever but GitHub right now has a powerful session giant so very often I use GitHub to search other open source project to see what they do how they do what they need to do so as you guess I need to look here QML it's very interesting but I think this is required to understand this new markup language so it's better to try on your own because when I chose to do this talk was only to introduce you the tools because there is not so much tutorials or to get started in 5 minutes using QT with Python now you know what are the steps so the talk is finished for any question I'm here