I was trying to create some nested widgets within file widget however exit is appearing above open. I tried changing the index but still the problems persists any idea how to fix this?
#!/usr/bin/python3
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import messagebox
import sys
class GuiBaseClass():
def __init__(self,root):
# create widgets
self.root=root
root.title('GUIBASECLASS V0.1')
root.option_add('*tearOff', False)
self.menu=dict()
self.menubar = tk.Menu(root)
#File
menu_file = tk.Menu(self.menubar)
self.menubar.add_cascade(menu=menu_file,label='File',underline=0)
menu_file.add_separator()
menu_file.add_command(label='Exit', command=self.Exit,underline=0)
#Edit
menu_edit = tk.Menu(self.menubar)
self.menubar.add_cascade(menu=menu_edit,label='Edit',underline=0)
#Help
menu_help = tk.Menu(self.menubar)
menu_help.add_command(label='About', command=self.About, underline=0)
self.menubar.add_cascade(menu=menu_help,label ='Help',underline=0)
#config
root.config(menu=self.menubar)
self.menu['menubar'] = self.menubar
self.menu['File'] = menu_file
self.menu['Edit'] = menu_edit
self.menu['Help'] = menu_help
self.frame = ttk.Frame(root)
self.frame.pack(fill='both',expand=True)
# public functions
def mainLoop(self):
self.root.mainloop()
def getFrame(self):
return(self.frame)
def getMenu(self,entry):
if entry in self.menu:
return (self.menu[entry])
else:
# we create a new one
last = self.menu['menubar'].index('end')
self.menu[entry]= tk.Menu(self.menubar)
self.menu['menubar'].insert_cascade(
last, menu=self.menu[entry],label=entry)
return(self.menu[entry])
# private functions
def Exit(self,ask=True):
res = tk.messagebox.askyesno(title='Are you sure',message='Really quit the application')
if res:
sys.exit(0)
pass
def About(self):
print('print GUIBASECLASS V0.1')
if __name__ == '__main__':
root=tk.Tk()
bapp = GuiBaseClass(root)
# example for using the BaseClass in other applications
mnu=bapp.getMenu('Edit')
mnu.add_command(label='Copy',command=lambda: print('Copy'))
# example for using getFrame
frm=bapp.getFrame()
btn=ttk.Button(frm,text="Button X",command=lambda: sys.exit(0))
btn.pack()
bapp.mainLoop()
#!/usr/bin/python3
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import filedialog
from tkinter import messagebox
from GuiBaseClass import GuiBaseClass
import os , sys
class PumlEditor(GuiBaseClass):
def __init__(self, root):
super().__init__(root)
# Create the File menu and add a "Open..." option
mnu_file = self.getMenu('File')
mnu_file.add_command(0, label='Open...', underline=0, command=self.fileOpen)
mnu_file.add_separator()
# Create the main text area
frame = self.getFrame()
#insert tk.Text
self.text = tk.Text(frame, wrap='word', undo=True)
self.text.insert('end', 'Hello start writing, please...')
self.text.delete('1.0','end')
self.text.pack(fill='both', expand=True)
def fileOpen(self, filename=''):
# Open a file dialog to select a file to open
if filename == "":
filename = filedialog.askopenfilename()
if filename != "":
self.text.delete('1.0','end')
file = open(filename,'r')
for line in file:
self.text.insert('end',line)
if __name__ == '__main__':
root = tk.Tk()
root.geometry("300x200")
pedit = PumlEditor(root)
# Create the PumlEditor instance and start the main loop
root.title("PumlEditor 2022")
if len(sys.argv)>1:
if os.path.exits(sys.arg[1]):
pedit.fileOpen(sys.argv[1])
pedit.mainLoop()
Open to appear before exit in files widget
When concatenating two video files, the PyQt window is completely black with no widgets. The background color changes to grey and the widget shows up only after the videos have been concatenated.
I would like to see a grey window and my widget while the videos are being concatenated.
Here is the script:
import os
import sys
from PyQt5.QtWidgets import *
from moviepy.editor import VideoFileClip, concatenate_videoclips
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Using Labels")
self.setGeometry(50,50,350,350)
self.UI()
def UI(self):
text1=QLabel("Hello Python",self)
text1.move(50,50)
self.show()
def make_movie():
sep = os.sep
directory_stub = '.' + sep + 'src' + sep + "assets" + sep
clip1 = VideoFileClip(directory_stub + 'introoutro' + sep + "intro.mp4")
clip2 = VideoFileClip(directory_stub + 'introoutro' + sep + "outro.mp4")
final_clip = concatenate_videoclips([clip1,clip2])
final_clip.write_videofile("my_concatenation.mp4")
def main():
App = QApplication(sys.argv)
window=Window()
make_movie()
sys.exit(App.exec_())
if __name__ == '__main__':
main()
While the videos are being put together I see this:
I want to see this during the video processing but I only see it 1. after the processing is done or 2. if I edit the code to never call make_movie():
Thanks for any help.
Concatenation is a very time consuming task so it should not be run on the main thread as it can freeze the GUI:
import os
import sys
import threading
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
from moviepy.editor import VideoFileClip, concatenate_videoclips
class QMovie(QObject):
started = pyqtSignal()
finished = pyqtSignal()
def concatenate(self, inputs, output):
threading.Thread(
target=self._concatenate, args=(inputs, output), daemon=True
).start()
def _concatenate(self, inputs, output):
self.started.emit()
clips = []
for input_ in inputs:
clip = VideoFileClip(input_)
clips.append(clip)
output_clip = concatenate_videoclips(clips)
output_clip.write_videofile(output)
self.finished.emit()
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Using Labels")
self.setGeometry(50, 50, 350, 350)
self.UI()
def UI(self):
text1 = QLabel("Hello Python", self)
text1.move(50, 50)
self.show()
def main():
App = QApplication(sys.argv)
window = Window()
directory_stub = os.path.join(".", "src", "assets")
in1 = os.path.join(directory_stub, "introoutro", "intro.mp4")
in2 = os.path.join(directory_stub, "introoutro", "outro.mp4")
qmovie = QMovie()
qmovie.concatenate([in1, in2], "my_concatenation.mp4")
sys.exit(App.exec_())
if __name__ == "__main__":
main()
I want to display the logged in user os.getlogin() displayed in a label widget.
So far I need to set a custom signal and connect it to the label
here is my (not functioning) code, hope you can guide my how to write it properly
import sys
import os
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
class UserTest(qtw.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# your code will go here
user_in_label = qtw.QLabel()
layout = qtw.QVBoxLayout()
layout.addWidget(user_in_label)
# funktionen
self.trigger = qtc.pyqtSignal(str)
# fire signal to slot
self.set_signal.connect(user_in_label.setText)
# your code ends here
self.show()
def set_signal(self):
active_user = os.getlogin()
self.trigger.emit(active_user)
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
w = UserTest()
sys.exit(app.exec_())
self.set_signal.connect(user_in_label.setText)
this line is missing an object, do I need to set a new class for this take, seems like nonsense
Try it:
import sys
import os
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
class UserTest(qtw.QWidget):
trigger = qtc.pyqtSignal(str) # +++
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
user_in_label = qtw.QLabel()
layout = qtw.QVBoxLayout(self) # + self
layout.addWidget(user_in_label)
layout.addWidget(qtw.QPushButton('Click me', clicked=self.set_signal)) # +++
self.trigger.connect(user_in_label.setText) # +++
def set_signal(self):
active_user = os.getlogin()
self.trigger.emit(active_user)
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
w = UserTest()
w.show()
sys.exit(app.exec_())
Update
can you explain why you relocate trigger = qtc.pyqtSignal(str) above init ? and any suggestions how to set active user automatically to the label without the push button?
trigger must be a class attribute
import sys
import os
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
class UserTest(qtw.QWidget):
trigger = qtc.pyqtSignal(str)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
user_in_label = qtw.QLabel()
layout = qtw.QVBoxLayout(self)
layout.addWidget(user_in_label)
# layout.addWidget(qtw.QPushButton('Click me', clicked=self.set_signal))
self.trigger.connect(user_in_label.setText)
self.set_signal() # +++
def set_signal(self):
active_user = os.getlogin()
self.trigger.emit(active_user)
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
w = UserTest()
w.show()
sys.exit(app.exec_())
This is the code.
I am using pyCharm and python 3.7
I am able to get the GUI with all the buttons but when i click on the button it won't show in the display box until I click on the display box that is if i click on "5" it won't show in the display box until i click on the display box.
I am using MacOsCatalina.
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QGridLayout
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QPushButton
from functools import partial
__version__ = "0.1"
ERROR_MSG = "ERROR"
>The Main GUI class
class calcgui(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("calc")
self.setFixedSize(300,300)
self.generalLayout = QVBoxLayout()
self._centralWidget = QWidget(self)
self.setCentralWidget(self._centralWidget)
self._centralWidget.setLayout(self.generalLayout)
self._createDisplay()
self._createButtons()
def _createDisplay(self):
self.display = QLineEdit()
self.display.setFixedHeight(50)
self.display.setAlignment(Qt.AlignRight)
self.display.setReadOnly(True)
self.generalLayout.addWidget(self.display)
def _createButtons(self):
self.buttons = {}
buttonsLayout = QGridLayout()
buttons = {"7":(0,0),
"8":(0,1),
"9":(0,2),
"C":(0,3),
"/":(0,4),
"4":(1,0),
"5":(1,1),
"6":(1,2),
"*":(1,3),
"(":(1,4),
"1":(2,0),
"2":(2,1),
"3":(2,2),
"-":(2,3),
")":(2,4),
"0":(3,0),
"00":(3,1),
".":(3,2),
"+":(3,3),
"=":(3,4)
}
for btnText, pos in buttons.items():
self.buttons[btnText] = QPushButton(btnText)
self.buttons[btnText].setFixedSize(50,50)
buttonsLayout.addWidget(self.buttons[btnText],pos[0],pos[1])
self.generalLayout.addLayout(buttonsLayout)
def setDisplayText(self, text):
self.display.setText(text)
self.display.setFocus()
def DisplayText(self):
return self.display.text()
def clearDisplay(self):
self.setDisplayText("")
>This is the linking class
class pycalcu:
def __init__(self,model,view):
self._evaluate = model
self._view = view
self._connectSignals()
def _calculateResult(self):
result = self._evaluate(expression=self._view.DisplayText())
self._view.setDisplayText(result)
def _buildExpression(self,sub_exp):
if self._view.DisplayText() == ERROR_MSG:
self._view.clearDisplay()
expression = self._view.DisplayText() + sub_exp
self._view.setDisplayText(expression)
def _connectSignals(self):
for btnText, btn in self._view.buttons.items():
if btnText not in {"C","="}:
btn.clicked.connect(partial(self._buildExpression,btnText))
self._view.buttons["="].clicked.connect(self._calculateResult)
return self._view.display.returnPressed.connect(self._calculateResult)
self._view.buttons["C"].clicked.connect(self._view.clearDisplay)
def evaluateExpression(expression):
try:
result = str(eval(expression, {}, {}))
except Exception:
result = ERROR_MSG
return result
def main():
pycalc = QApplication(sys.argv)
view = calcgui()
view.show()
model = evaluateExpression
pycalcu(model=model,view=view)
sys.exit(pycalc.exec_())
if __name__ == "__main__":
main()
Sorry, I didn’t really delve into your program logic.
I noted the changes that I made.
Give it a try.
import sys
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget, QMainWindow,
QGridLayout, QVBoxLayout, QLineEdit, QPushButton)
from PyQt5.QtCore import Qt
from functools import partial
__version__ = "0.1"
ERROR_MSG = "ERROR"
# >The Main GUI class
class calcgui(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("calc")
self.setFixedSize(300,300)
self._centralWidget = QWidget(self)
self.setCentralWidget(self._centralWidget)
self.generalLayout = QVBoxLayout(self._centralWidget)
self._createDisplay()
self._createButtons()
def _createDisplay(self):
self.display = QLineEdit()
self.display.setFixedHeight(50)
self.display.setAlignment(Qt.AlignRight)
self.display.setReadOnly(True)
self.generalLayout.addWidget(self.display)
def _createButtons(self):
self.buttons = {}
buttonsLayout = QGridLayout()
buttons = {
"7":(0,0),
"8":(0,1),
"9":(0,2),
"C":(0,3),
"/":(0,4),
"4":(1,0),
"5":(1,1),
"6":(1,2),
"*":(1,3),
"(":(1,4),
"1":(2,0),
"2":(2,1),
"3":(2,2),
"-":(2,3),
")":(2,4),
"0":(3,0),
"00":(3,1),
".":(3,2),
"+":(3,3),
"=":(3,4)
}
for btnText, pos in buttons.items():
self.buttons[btnText] = QPushButton(btnText)
self.buttons[btnText].setFixedSize(50,50)
buttonsLayout.addWidget(self.buttons[btnText],pos[0],pos[1])
# <----
self.generalLayout.addLayout(buttonsLayout) # <----
def setDisplayText(self, text):
self.display.setText(text)
self.display.setFocus()
def DisplayText(self):
return self.display.text()
def clearDisplay(self):
self.setDisplayText("")
# >This is the linking class
class pycalcu:
def __init__(self,model, view):
self._evaluate = model
self._view = view
self._connectSignals()
def _calculateResult(self):
result = self._evaluate(expression=self._view.DisplayText())
self._view.setDisplayText(result)
def _buildExpression(self, sub_exp):
if self._view.DisplayText() == ERROR_MSG:
self._view.clearDisplay()
expression = self._view.DisplayText() + sub_exp
self._view.setDisplayText(expression)
def _connectSignals(self):
for btnText, btn in self._view.buttons.items():
if btnText not in {"C", "="}:
btn.clicked.connect(partial(self._buildExpression, btnText))
self._view.buttons["="].clicked.connect(self._calculateResult)
self._view.buttons["C"].clicked.connect(self._view.clearDisplay) # +++
return self._view.display.returnPressed.connect(self._calculateResult)
# ? self._view.buttons["C"].clicked.connect(self._view.clearDisplay) # ---
def evaluateExpression(expression):
try:
result = str(eval(expression, {}, {}))
except Exception:
result = ERROR_MSG
return result
def main():
pycalc = QApplication(sys.argv)
view = calcgui()
view.show()
model = evaluateExpression
pycalcu(model=model, view=view)
sys.exit(pycalc.exec_())
if __name__ == "__main__":
main()
I've converted a png file like this with base64
b'iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAAangAAGp4B8NQjJQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAANRSURB\nVGiB7dpPqFVVFMfxz3tKVGS97EEGGYSZORGKJxHUw/4QgeNoVDTMAiuhoSAIgaD9/wMNhBo0qCZN\nghpIf0ZiDeRFFBJBEpFWgqG+euptsN957Hc853ruOeteL+IPFhzOvay1v/vss/beax+uaLw0Eezv\nGmzGpkWbKv1+AnM4jG9xJjh+J92CF/A15tFraPP4Cs9jzchbnelOfICzmje+zs7ifdwxSoDr8RoW\nAgDKtoB9WDVsiEfw2xAAynYUDw4LYhfOjQCisHPYGQkwgTdGCFC2VwVl13cvIURh73SFeGkMIAp7\nsS3EFjGp9TiexlrciifxRws/C3hgUIgbxGSnf3FPhf9N0qw+qL+jUvpvrDcDIHr4tE+MT1r6fKUp\nxAYxQ6onJYo6vd7S5wLWNQHZHwTRw/dYWRFjhbRwbOv3vYtBTEvjOgqkJ/V8DrMCezv6nMfqfiDb\ngyEKm8Nb0rvX5Unk9mw/kG+GBDIM+zJveD71r8Lfqsf0OOo/aXidYnmjN4uDOIID+FWak+7CY7gq\nyL9FXzPS5myZntPtUR+TcvzdNYE34OeOMcq2rSpQ27xe2DM1ALnuDQZZmhwnsyA3NWhIPz0hvWM9\n/ODCwgMcxO8d4+SaLi5ykOs6Ot2CGxevN0q9X6V/OsbJtbQlzkGiS0OnKu5N4ObAGEttzkEie+q8\nNLzKuk3KYlE6WVzkIH8FBpiT3peyZgNjwJ/FRQ7yU2CAz2rubw2MQdbmHOTHwACfV9ybxMOBMahp\n87UGK3nW2QnVK4T7AnzndgZXF87zJ3Iahxr2RD8dkDZmZT0a4DvXQanjsRwEPg4I8EXN/YcCfOf6\nqN+PERurmQq/k9ITjxpWF91Y0X2rO32hS9OBED0Ntrp0Lz5UlWtWSqvj4j/nta8jNy4+0K0cVJdi\n1+IpaXG5UftjicblILoV6A7rf74xJVX22/geuEBHt5LpL9KR2qw0DNbjcXyoXYWxGFIDl0wLXRZF\n7EIvjwHE7q4QpDX/vksIsUfwXmmH4RyA9nsntkcC5Jo1usPQ+4cFUeiyOJ7OtU46NmibTnM7jbdx\n+ygBypqSCsptP+HYprp0NJCG8VHNjFQiXS+tUIuZ+KRUFzgi7ey+M2Yf1VxRpP4H+dEoOtaum3IA\nAAAASUVORK5CYII=\n'
I have a GUİ to convert this code to and image again but when I enter it and take it with .toPlainText() it turns nothing.
It works if I manually type in the script:
How can I define a variable to fix that?
The problem is that toPlainTex() returns a string that in reality must be a bytearray, a way to convert it is using ast.literal() as I show below:
import ast
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
lay = QtWidgets.QVBoxLayout(self)
self.textEdit = QtWidgets.QTextEdit()
self.label = QtWidgets.QLabel()
self.label.setAlignment(QtCore.Qt.AlignCenter)
button = QtWidgets.QPushButton("convert")
lay.addWidget(self.textEdit)
lay.addWidget(button)
lay.addWidget(self.label)
button.clicked.connect(self.on_clicked)
def on_clicked(self):
text = self.textEdit.toPlainText()
try:
data = ast.literal_eval(text)
ba = QtCore.QByteArray.fromBase64(data)
pixmap = QtGui.QPixmap()
if pixmap.loadFromData(ba, "PNG"):
self.label.setPixmap(pixmap)
except SyntaxError:
pass
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())