GUI under embedded Linux / python 3.3.2 - linux

I'm writing a GTK+ app using embedded/extended Python3 as a scripting language. What I'd like do is make it possible for those scripts to include menus, but, so far, things aren't cooperating.
I do the usual python initialisation with:
PyImport_AppendInittab("gfig", &PyInit_gfig);
Py_SetProgramName (L"gfig");
Py_Initialize();
and then some setup with:
PyRun_SimpleString (
"import gfig\n"
"import sys\n" ........ )
(gfig is the app I'm writing, as well as the name of extension module.)
Then I run the app using
PyRun_File (fp, pyfile, Py_file_input, global_dict, local_dict);
to run the script:
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World")
self.button = Gtk.Button(label="Click Here")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
def on_button_clicked(self, widget):
print("Hello World")
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
This results in:
[0] /mnt/disk5/personal/tinkering/gf3/src >./gf py4
Traceback (most recent call last): File "py4", line 3, in
from gi.repository import Gtk File "",
line 1567, in _find_and_load File "",
line 1534, in _find_and_load_unlocked File "/usr/lib64/python3.3/site-packages/gi/importer.py",
line 68, inload_module dynamic_module._load() File "/usr/lib64/python3.3/site-packages/gi/module.py",
line 289, in _load self._overrides_module = importlib.import_module('gi.overrides.' +
self._namespace) File "/usr/lib64/python3.3/importlib/init.py",
line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/usr/lib64/python3.3/site-packages/gi/overrides/Gtk.py",
line 1523,in initialized, argv = Gtk.init_check(sys.argv)
AttributeError 'module' object has no attribute 'argv'
Here's the script:
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World")
self.button = Gtk.Button(label="Click Here")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
def on_button_clicked(self, widget):
print("Hello World")
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
which works fine when from the command line:
[0] /mnt/disk5/personal/tinkering/gf3/src >python3 py4
I see that the script ends with "Gtk.main()" which, since I'm running python embedded under GTK+, looks like it may give me trouble in the future.
(I've tried Tk, wx, and, I think, Qt. None of them worked, but I didn't explore why not in any detail. I'd like to use Gtk.)
Any help greatly appreciated.

Related

Python script doesn't recognize 'self' argument in PyQt MainWindow

I'm trying to get a PyQt lineEdit widget to place its' contents within a variable in the module 'followup' but for some reason the module does not recognize the PyQt MainWindow as being properly instantiated.
#workbot_main.py
import gspread
import os
import sys
sys.path.append(os.path.abspath(r"C:\Users\leamy\Desktop\work bot\workbot.py"))
sys.path.append(os.path.abspath(r"C:\Users\leamy\Desktop\work bot\initial.py"))
sys.path.append(os.path.abspath(r"C:\Users\leamy\Desktop\work bot\followup.py"))
import threading
import followup
import initial
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5.QtWidgets import QMainWindow, QApplication
from workbot import *
import workbot
#workbot.py is the QTdesigner file containing the widgets,
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
followup_start_button = self.ui.followup_start_button
followup_start_button.clicked.connect(threads.begin_initialize)
def followup_starting_number(self):
num = self.ui.lineEdit.text()
return num
def begin_initialize(self):
t1 = threading.Thread(target = followup.initialize, args = ())
t1.start()
threads = Threads()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
widget = MainWindow()
widget.show()
app.exec_()
#followup.py
import workbot_main
def initialize():
row_number.number = workbot_main.MainWindow.followup_starting_number()
print(row_number.number)
I get this error
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\leamy\anaconda3\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\Users\leamy\anaconda3\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\leamy\Desktop\work bot\followup.py", line 313, in initialize
row_number.number = workbot_main.MainWindow.followup_starting_number()
TypeError: followup_starting_number() missing 1 required positional argument: 'self'
I can't understand why it doesn't work.
Edit:
If I try to use
row_number.number = workbot_main.widget.followup_starting_number()
instead of
row_number.number = workbot_main.MainWindow.followup_starting_number()
I get this error
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\leamy\anaconda3\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\Users\leamy\anaconda3\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\leamy\Desktop\work bot\followup.py", line 314, in initialize
row_number.number = workbot_main.widget.followup_starting_number()
AttributeError: module 'workbot_main' has no attribute 'widget'
I don't know if that is related.

When using the Button object in Tkinter I get an error

When running this code:
import tkinter as tk
class MyApplication(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title('Programma')
self.geometry('500x400')
self.button = tk.Button(self, text='Execute', command=self.fun)
self.button.grid(row=0, column=4, stick=tk.W)
def fun(self):
print('Hello')
app = MyApplication()
app.mainloop()
I get the following error:
Traceback (most recent call last): File "C:/Users/Andrea/Desktop/hfhfhg.py", line 17, in <module>
app = MyApplication() File "C:/Users/Andrea/Desktop/hfhfhg.py", line 10, in __init__
self.button = tk.Button(self, text='Execute', command=self.fun) File "C:\WPy64-3910\python-3.9.1.amd64\lib\tkinter\__init__.py", line 2346, in __getattr__
return getattr(self.tk, attr) AttributeError: '_tkinter.tkapp' object has no attribute 'fun'
I understand that the code is not optimal for a GUI but I would like to understand how Tkinter works without using a frame object. Please can you help me?

Ttk style layout not found how to fix?

I am making my own custom Menubar.My work is in process but i recently came across one confusion.
Here is my code:-
from tkinter import *
from tkinter.ttk import Style,Frame as fp
class menu():
def __init__(self,parent,bg="#ffffcc"):
self.parent = parent
self.bg = bg
#this is a image in base 64 encoded format
borderImageData='''
iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAABmJLR0QA/wD/
AP+gvaeTAAACZ0lEQVRoge2aPU/TURSHn/MvpbYYKa9qQQnlTSOJRBzoYBDi
YEwcdPFD+PIFHHRxcCXxO7gxmBgXSTcxMYQEE4spoEWIvBcDBfp2HVqlxRhK
TDwtuc947vJ7cnPvGc6R/v5+KhlHO8C/YgW0sQLaWAFtqg4WxKEjJMGQaQyK
txbHpZGqmGzG7GzKyoyZfcfsOCZbeChFjaz+vAw/oq6V1K75HiERJ5v+33H/
xKnC55czF3CfYD1mxkbY+Pb7sECguUtuPQbMh5d8eksmpZL2r7jccvEGV++B
Ma+fsRzNlwOBAID3lNx+gjHm1VNiEweuqSwwWZajxCak85oEQ3wOk0my/4j7
7uD1m7ERNuY1Ux7KeozwC3x+uXI3V3AAXFXSPcjCFAtTmuFKw8xPsviRrsHc
B+MANHVS7WPuvXK0kjGz43hqaOogL3CyASC+oJrqKMQXAWoayQmI2wuY1J5q
qKOQ2gXE4+MYdGIroI0V0MYKaGMFtLEC2lgBbayANlZAGyugjRXQxgpoYwW0
sQLaWAFtrIA2DmCSCQC3RzlL6VR7AZIJ8jewtQaIv0Uz05GoPQuYrVXyAqsz
JBO0D+imKh0JhtjbYmWGvEAmbabDtPTSelk5Wimc6yNwiekw2Qz7j3hylERc
hh9S36YZ7jCkoU2GHpjtdTM5mqv8mtSnkyxFpHtQeoZIJ1n7UnbDelc1vTfl
+n1EePOcH0u5cvGyR11r/hJSeyxFzPZGmSx7SE09p3twe1j7asZGCifCcnDt
UhzaBwgOSHMnZbNuw86mWY7K3Lg5ZN2mAjkWnbiisQLaWAFtKl7gJzIjtMOb
uqQwAAAAAElFTkSuQmCC
'''
self.borderImage = PhotoImage( data=borderImageData,master=self.parent)
self.TP_style=Style()
self.TP_style.element_create("RoundedFrame",
"image", self.borderImage,
border=14, sticky="nsew")
self.TP_style.layout("RoundedFrame",
[("RoundedFrame", {"sticky": "nsew"})])
self.frame_one = fp(parent, style="RoundedFrame", padding=10,width=100,height=100)
self.frame_one.pack()
l1=Label(parent,image=self.borderImage).pack()
def popup(self,x,y,width=110,height=120):
self.width=width
self.height=height
self.app = Tk()
self.app.config(bg=self.bg)
self.app.geometry(f"{self.width}x{self.height}+{x}+{y}")
self.app.wm_attributes("-topmost",True)
self.app.overrideredirect(True)
self.app.focus_force()
#self.frame_one = fp(self.app, style="RoundedFrame", padding=10,width=100,height=100)
#self.frame_one.pack()
#l1=Label(self.app,image=self.borderImage).pack()
#self.m.pack_propagate(0)
def destroy(event):
self.app.destroy()
self.app.bind("<FocusOut>",destroy)
if __name__ == "__main__":
root = Tk()
menu = menu(root)
def evt(evt):
menu.popup(evt.x_root,evt.y_root,width=200,height=400)
root.bind("<Button-3>",evt)
root.mainloop()
If i write style for frame like
self.frame_one = fp(parent, style="RoundedFrame", padding=10,width=100,height=100)
in the init() method every thing works fine. If same thing i write in popup methon(where i have just commented out),i got the following error.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\mishra\P_Menu.py", line 64, in evt
menu.popup(evt.x_root,evt.y_root,width=200,height=400)
File "C:\Users\mishra\P_Menu.py", line 51, in popup
self.frame_one = fp(self.app, style="RoundedFrame", padding=10,width=100,height=100)
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\ttk.py", line 740, in __init__
Widget.__init__(self, master, "ttk::frame", kw)
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\ttk.py", line 557, in __init__
tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__
self.tk.call(
_tkinter.TclError: Layout RoundedFrame not found
Same thing for Lable
l1=Label(self.app,image=self.borderImage).pack()
In the init() section everything works fine but in popup method I got following error.
Traceback (most recent call last):
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\mishra\P_Menu.py", line 64, in evt
menu.popup(evt.x_root,evt.y_root,width=200,height=400)
File "C:\Users\mishra\P_Menu.py", line 53, in popup
l1=Label(self.app,image=self.borderImage).pack()
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3143, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__
self.tk.call(
_tkinter.TclError: image "pyimage1" doesn't exist
I want to know how to fix it,cause i want to know logic behind it and use in popup() method.
Any help will be appriciated.
Thank You!
Maybe it's because popup creates a new Tk instance as self.app, but the style belongs to the first Tk instance created by root = Tk(). So a child of the second Tk instance cannot recognize it. Maybe you can try changing self.app = Tk() to self.app = Toplevel() to see if it works?

Pyinstaller "ValueError: Can't mix absolute and relative paths"

I am using windows 10 and anaconda3 to manage my python packages. This is my first time to use python, and I'm trying to make my own gui program with pyqt5. Also I'm trying to make .exe file using Pyinstaller. The issue I am running into is that the .exe is throwing the error block:
(pyqt5_env) C:\Python Projects>pyinstaller -w -F App_ver05.py
268 INFO: PyInstaller: 4.0.dev0+b3dd91c8a8
268 INFO: Python: 3.7.7 (conda)
268 INFO: Platform: Windows-10-10.0.18362-SP0
Traceback (most recent call last):
File "c:\anaconda3\envs\pyqt5_env\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\anaconda3\envs\pyqt5_env\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Anaconda3\envs\pyqt5_env\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
File "c:\anaconda3\envs\pyqt5_env\lib\site-packages\PyInstaller\__main__.py", line 112, in run
spec_file = run_makespec(**vars(args))
File "c:\anaconda3\envs\pyqt5_env\lib\site-packages\PyInstaller\__main__.py", line 58, in run_makespec
spec_file = PyInstaller.building.makespec.main(filenames, **opts)
File "c:\anaconda3\envs\pyqt5_env\lib\site-packages\PyInstaller\building\makespec.py", line 458, in main
specfile.write(onefiletmplt % d)
File "c:\anaconda3\envs\pyqt5_env\lib\site-packages\PyInstaller\building\makespec.py", line 101, in __repr__
self.variable_prefix, self.filename_suffix = make_variable_path(self.path)
File "c:\anaconda3\envs\pyqt5_env\lib\site-packages\PyInstaller\building\makespec.py", line 84, in make_variable_path
if os.path.commonpath([filename, from_path]) == from_path:
File "c:\anaconda3\envs\pyqt5_env\lib\ntpath.py", line 615, in commonpath
raise ValueError("Can't mix absolute and relative paths") from None
ValueError: Can't mix absolute and relative paths
The same error occurs regardless of which .py file is used. For information, I wrote the used code below.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn1 = QPushButton('&Button1', self)
btn1.setCheckable(True)
btn1.toggle()
vbox = QVBoxLayout()
vbox.addWidget(btn1)
self.setLayout(vbox)
self.setWindowTitle('QPushButton')
self.setGeometry(300, 300, 300, 200)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
I uninstalled and reinstalled pyinstaller but it didn't work. I don't think its a code issue because the code is really simple. Can anyone give a solution or hint for this problem?
What happened for me is that the absolute path name had a space in it. Once the project was moved to a directory without spaces it was able to build
The solution of this problem is to use the full file name and append the flag -F to the file name. In your case for example if for the file App_ver05.py the absolute path is :
/home/user123/Desktop/foldername/App_ver05.py
Then use the command :
pyinstaller -F /home/user123/Desktop/foldername/App_ver05.py

Error to compile file PyObject (Gtk ) with Pydev

I'm build application with pyobject (gtk) in Pydev , but i'm try to create a window
the following way:
Class Ventana
from gi.overrides import Gtk
class Ventana( Gtk.Window ):
def __init__( self,titulo ):
Gtk.Window.__init__(self,title=titulo )
self.connect("delete-event",Gtk.main_quit )
Class Aplicacion( Main):
def main():
win = Ventana()
if __name__ == '__main__':
main()
but I'm try to compile te App, show the following mistake:
Traceback (most recent call last):
File "/home/demian/workspace/NidhugsRPG/nidhugs/presentacion/Consola.py", line 6, in
<module>
from gi.overrides import Gtk
File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 35, in <module>
Gtk = modules['Gtk']._introspection_module
KeyError: 'Gtk'
I'm try use the import
from gi.repository import Gtk
but, not works,because moudule not found.So i'm use the import:
from gi.overrides import Gtk
¿How to fix my trouble?
Sorry for my bad English
Thanks.

Resources