Posts: 4
Threads: 4
Joined: Jul 2020
Jul-21-2020, 01:49 PM
(This post was last modified: Jul-21-2020, 01:49 PM by yan_mhb.)
Hello,
I have a project in Python and I want to create an application.
the content of the Graphical interface is to create several windows for example:
the first window will contain 7 buttons and each button will direct me to another window and on each window there will be buttons like open and close which will go to my python code and change the value, or change the value of a CAN frame.
I found several libraries like tkinter or PyQt5 I don't know which one to choose
is all this feasible or not?
Thanks for your help
Posts: 1,028
Threads: 16
Joined: Dec 2016
(Jul-21-2020, 01:49 PM)yan_mhb Wrote: I found several libraries like tkinter or PyQt5 I don't know which one to choose
try both and you will see what you like better.
Posts: 3
Threads: 0
Joined: Jul 2020
To make a Form in the python is a pain. Another pain is to populate the Form with controls and arrange them.
Why does it have to be so difficult?
Posts: 1,028
Threads: 16
Joined: Dec 2016
(Jul-21-2020, 08:09 PM)Cordial Wrote: To make a Form in the python is a pain. Another pain is to populate the Form with controls and arrange them.
Why does it have to be so difficult?
Sorry, bit this is nonsense
Posts: 1,093
Threads: 143
Joined: Jul 2017
Jul-22-2020, 12:47 AM
(This post was last modified: Jul-22-2020, 12:47 AM by Pedroski55.)
I'm just an amateur, so maybe my way of doing this is not the best, but I just did this, and it works.
I even made an icon this morning to launch the main window from Ubuntu's launcher!
Have a look here.
If you have any questions, just ask.
Posts: 3
Threads: 0
Joined: Jul 2020
Well, I have to make a graphic animation and I need a drawing surface (canvas I suppose) that is on the left side of the Form, and on the right are the controls and buttons OK, Cancel.
All this can be done urgently in VS using C#, but extremely difficult in Python.
There is no Drag and Drop option for controls.
And what is Tkinter or IronPython? Why doesn't Phyton come with all the tools and you have to install other things?
I looked on YouTube to find something similar to what I want and I didn't find anything.
Is there a video to explain this?
Posts: 1,093
Threads: 143
Joined: Jul 2017
@Cordial: I have to say I can't agree with you. If I can do it, anyone can: I am useless at programming!
I'm sure tkinter has all the tools you need.
Posts: 1,028
Threads: 16
Joined: Dec 2016
In PyQt5 you can place your toolbars wherever you want. (to, bottom, left, right)
An example
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#####################################################################
import os
from PyQt5.QtCore import (QFile, QDir, QFileInfo, QPoint, QRect, QSettings, QSize,
Qt, QTextStream, QCoreApplication)
from PyQt5.QtGui import QIcon, QKeySequence
from PyQt5.QtWidgets import (QAction, QApplication, QFileDialog, QMainWindow,
QToolBar, QMessageBox, QTextEdit)
myCompany = "myCompany"
myApp = "myApp"
homepath = QDir.homePath()
#####################################################################
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.root = QFileInfo.path(QFileInfo(QCoreApplication.arguments()[0]))
print("Welcome ...\nroot is " + self.root + "/")
self.curFile = ''
self.settings = QSettings(myCompany, myApp)
self.textEdit = QTextEdit()
self.setCentralWidget(self.textEdit)
self.createActions()
self.createMenus()
self.createToolBars()
self.createStatusBar()
self.readSettings()
self.textEdit.document().contentsChanged.connect(self.documentWasModified)
self.setCurrentFile('')
self.setWindowTitle(myApp + '[*]')
self.setGeometry(10, 10, 800, 600)
def closeEvent(self, event):
if self.maybeSave():
self.writeSettings()
event.accept()
else:
event.ignore()
def newFile(self):
if self.maybeSave():
self.textEdit.clear()
self.setCurrentFile('')
def open(self):
if self.maybeSave():
fileName, _ = QFileDialog.getOpenFileName(self, "", homepath)
if fileName:
self.loadFile(fileName)
def save(self):
if self.curFile:
return self.saveFile(self.curFile)
return self.saveAs()
def saveAs(self):
fileName, _ = QFileDialog.getSaveFileName(self)
if fileName:
return self.saveFile(fileName)
return False
def about(self):
QMessageBox.about(self, "About myApp",
"<center><b>myApplication</b><br>1.0<br>"
"© 2018</center>")
def documentWasModified(self):
self.setWindowModified(self.textEdit.document().isModified())
def createActions(self):
root = QFileInfo(__file__).absolutePath()
self.newAct = QAction(QIcon.fromTheme('document-new'), "&New", self,
shortcut=QKeySequence.New, statusTip="Create a new file",
triggered=self.newFile)
self.openAct = QAction(QIcon.fromTheme('document-open'), "&Open...",
self, shortcut=QKeySequence.Open,
statusTip="Open an existing file", triggered=self.open)
self.saveAct = QAction(QIcon.fromTheme('document-save'), "&Save", self,
shortcut=QKeySequence.Save,
statusTip="Save the document to disk", triggered=self.save)
self.saveAsAct = QAction(QIcon.fromTheme('document-save-as'),"Save &As...", self,
shortcut=QKeySequence.SaveAs,
statusTip="Save the document under a new name",
triggered=self.saveAs)
self.exitAct = QAction(QIcon.fromTheme('application-exit'),"E&xit", self, shortcut="Ctrl+Q",
statusTip="Exit the application", triggered=self.close)
self.cutAct = QAction(QIcon.fromTheme('edit-cut'), "Cu&t", self,
shortcut=QKeySequence.Cut,
statusTip="Cut the current selection's contents to the clipboard",
triggered=self.textEdit.cut)
self.copyAct = QAction(QIcon.fromTheme('edit-copy'), "&Copy", self,
shortcut=QKeySequence.Copy,
statusTip="Copy the current selection's contents to the clipboard",
triggered=self.textEdit.copy)
self.pasteAct = QAction(QIcon.fromTheme('edit-paste'), "&Paste",
self, shortcut=QKeySequence.Paste,
statusTip="Paste the clipboard's contents into the current selection",
triggered=self.textEdit.paste)
self.aboutAct = QAction(QIcon.fromTheme('help-info'),"&About", self,
statusTip="Show the application's About box",
triggered=self.about)
self.aboutQtAct = QAction(QIcon.fromTheme('help-about'),"About &Qt", self,
statusTip="Show the Qt library's About box",
triggered=QApplication.instance().aboutQt)
self.cutAct.setEnabled(False)
self.copyAct.setEnabled(False)
self.textEdit.copyAvailable.connect(self.cutAct.setEnabled)
self.textEdit.copyAvailable.connect(self.copyAct.setEnabled)
def createMenus(self):
self.fileMenu = self.menuBar().addMenu("&File")
self.fileMenu.addAction(self.newAct)
self.fileMenu.addAction(self.openAct)
self.fileMenu.addAction(self.saveAct)
self.fileMenu.addAction(self.saveAsAct)
self.fileMenu.addSeparator();
self.fileMenu.addAction(self.exitAct)
self.editMenu = self.menuBar().addMenu("&Edit")
self.editMenu.addAction(self.cutAct)
self.editMenu.addAction(self.copyAct)
self.editMenu.addAction(self.pasteAct)
self.menuBar().addSeparator()
self.helpMenu = self.menuBar().addMenu("&Help")
self.helpMenu.addAction(self.aboutAct)
self.helpMenu.addAction(self.aboutQtAct)
def createToolBars(self):
self.fileToolBar = QToolBar("File")
self.addToolBar(Qt.RightToolBarArea, self.fileToolBar)
self.fileToolBar.setIconSize(QSize(16, 16))
self.fileToolBar.setContextMenuPolicy(Qt.PreventContextMenu)
self.fileToolBar.setMovable(False)
self.fileToolBar.addAction(self.newAct)
self.fileToolBar.addAction(self.openAct)
self.fileToolBar.addAction(self.saveAct)
self.fileToolBar.addAction(self.saveAsAct)
self.fileToolBar.addSeparator()
self.editToolBar = QToolBar("Edit")
self.addToolBar(Qt.RightToolBarArea, self.editToolBar)
self.editToolBar.setIconSize(QSize(16, 16))
self.editToolBar.setContextMenuPolicy(Qt.PreventContextMenu)
self.editToolBar.setMovable(False)
self.editToolBar.addAction(self.cutAct)
self.editToolBar.addAction(self.copyAct)
self.editToolBar.addAction(self.pasteAct)
def createStatusBar(self):
self.statusBar().setStyleSheet("font-size: 8pt; color: #888a85;")
self.statusBar().showMessage("Ready")
def readSettings(self):
pos = self.settings.value("pos")
size = self.settings.value("size")
self.resize(size)
self.move(pos)
def writeSettings(self):
self.settings.setValue("pos", self.pos())
self.settings.setValue("size", self.size())
def maybeSave(self):
if self.textEdit.document().isModified():
ret = QMessageBox.warning(self, "Application",
"The document has been modified.\nDo you want to save "
"your changes?",
QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)
if ret == QMessageBox.Save:
return self.save()
if ret == QMessageBox.Cancel:
return False
return True
def loadFile(self, fileName):
file = QFile(fileName)
if not file.open(QFile.ReadOnly | QFile.Text):
QMessageBox.warning(self, "Application",
"Cannot read file %s:\n%s." % (fileName, file.errorString()))
return
inf = QTextStream(file)
QApplication.setOverrideCursor(Qt.WaitCursor)
self.textEdit.setPlainText(inf.readAll())
QApplication.restoreOverrideCursor()
self.setCurrentFile(fileName)
self.statusBar().showMessage("File loaded", 2000)
def saveFile(self, fileName):
file = QFile(fileName)
if not file.open(QFile.WriteOnly | QFile.Text):
QMessageBox.warning(self, "Application",
"Cannot write file %s:\n%s." % (fileName, file.errorString()))
return False
outf = QTextStream(file)
QApplication.setOverrideCursor(Qt.WaitCursor)
outf << self.textEdit.toPlainText()
QApplication.restoreOverrideCursor()
self.setCurrentFile(fileName);
self.statusBar().showMessage("File saved", 0)
return True
def setCurrentFile(self, fileName):
self.curFile = fileName
self.textEdit.document().setModified(False)
self.setWindowModified(False)
if self.curFile:
shownName = self.strippedName(self.curFile)
else:
shownName = myApp
self.setWindowTitle(shownName + "[*]")
def strippedName(self, fullFileName):
return QFileInfo(fullFileName).fileName()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
Posts: 3
Threads: 0
Joined: Jul 2020
Jul-22-2020, 12:51 PM
(This post was last modified: Jul-22-2020, 12:51 PM by Cordial.)
@ Axel_Erfurt, thanks for the code!
That is automatically done by VS (in "backstage") in C# when you drag and drop the controls on the Form.
I need to learn about another thing: PyQt5.
And my next question is: How to make a dll file in Python? Is that possible?
Posts: 1,358
Threads: 2
Joined: May 2019
So @ Cordial, you want a graphical designer for your forms. There are some for most of the GUI platforms. I use one routinely if I want a GUI, using WX. Drag and drop design, and when complete click a button and it creates the code that will generate the screen. Modify that code to do the things you want and you are done.
It comes down to Python not endorsing one single GUI, rather allowing multiple GUIs to be developed. Eventually one may win out, but for now pick the one you like. TK, PyQT5, WX, whatever.
|