Error to compile file PyObject (Gtk ) with Pydev - python-3.x

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.

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.

Cyclic import in Python3

I have the following directory structure:
my-game/
__init__.py
logic/
__init__.py
game.py
player.py
game.py and player.py have import dependency on each other (cyclic imports).
game.py has the following definition.
from logic.player import RandomPlayer, InteractivePlayer
T = 8
class Game:
def __init__(self, p1, p2)
...
# some other things
if __name__ == '__main__':
p1 = RandomPlayer()
p2 = InteractivePlayer()
g = Game(p1, p2)
...
player.py is as follows:
from logic.game import T
class Player:
def __init__(self):
...
class RandomPlayer(Player):
def __init__(self):
...
class InteractivePlayer(Player):
def __init__(self):
...
I am trying to run the game from logic/ directory but I get the following error.
$ python3 game.py
Traceback (most recent call last):
File "game.py", line 2, in <module>
from logic.player import RandomPlayer, InteractivePlayer
ModuleNotFoundError: No module named 'logic'
I then tried running game.py from the directory higher up (my-game/).
$ python3 logic/game.py
Traceback (most recent call last):
File "logic/game.py", line 2, in <module>
from logic.player import RandomPlayer, InteractivePlayer
ModuleNotFoundError: No module named 'logic'
What am I doing wrong? How can I make these cyclic imports work?
I have also tried using this import in player.py
from .game import T
and using
from .player import RandomPlayer, InteractivePlayer
in game.py.
In this case, I get a different error. For example, when running from my-game/,
$ python3 logic/game.py
Traceback (most recent call last):
File "logic/game.py", line 2, in <module>
from .player import RandomPlayer, InteractivePlayer
ModuleNotFoundError: No module named '__main__.player'; '__main__' is not a package
I get a similar error when running from logic/ directory.
I looked at this post but didn't understand where I was going wrong.
You are made a circulair import in your code try to remove it from your import
Vist this link you can find some other info about circular import :Remove python circular import

ImportError: cannot import name 'itemgetter' from 'operator'

Help me with my problem, I've made some file that named 'percobaan.py' and I ran it with cmd and it's got some error. the error say ImportError: cannot import name 'itemgetter' from 'operator', But if I run my code in python IDLE the code runs without any errors.
My code:
import tkinter
def msg():
print("i'm learning tkinter built in")
root = tkinter.Tk()
label = tkinter.Label(root, text='welcome to the tkinter built in project')
label.pack()
label2 = tkinter.Label(root,text='choose your favorite programming languange',
justify=tkinter.CENTER,
padx=20)
label2.pack()
tkinter.Radiobutton(root, text='python',padx=20, value= 1).pack(anchor=tkinter.N)
tkinter.Radiobutton(root, text='Javascript',padx=20, value= 2).pack(anchor=tkinter.N)
tkinter.Radiobutton(root, text='C++',padx=20, value= 3).pack(anchor=tkinter.N)
tkinter.Radiobutton(root, text='C',padx=20, value= 4).pack(anchor=tkinter.N)
frame = tkinter.Frame(root)
frame.pack()
button = tkinter.Button(frame, text='quit',fg='black',command=quit)
button.pack(side=tkinter.LEFT)
slogan = tkinter.Button(frame, text='see the message',command=msg)
slogan.pack(side=tkinter.RIGHT)
root.mainloop()
and the error says:
Traceback (most recent call last):
File "GUI.py", line 1, in <module>
import tkinter
File "C:\Users\khairunnisa\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 39, in <module>
import re
File "C:\Users\khairunnisa\AppData\Local\Programs\Python\Python37-32\lib\re.py", line 125, in <module>
import functools
File "C:\Users\khairunnisa\AppData\Local\Programs\Python\Python37-32\lib\functools.py", line 21, in <module>
from collections import namedtuple
File "C:\Users\khairunnisa\AppData\Local\Programs\Python\Python37-32\lib\collections\__init__.py", line 21, in <module>
from operator import itemgetter as _itemgetter, eq as _eq
ImportError: cannot import name 'itemgetter' from 'operator' (D:\JOB\__python\exercise_oop\GUI\operator.py)
I've tried many times to run this code and the packages are already installed on my PC, but the result is still nothing.
any idea why?

Use button to call a file.py with glade

i'm trying through a python script in conjunction with glade, create a button that opens me a file in python so I can edit if I want to make some changes later. Can anyone help me if you please ?
What i did was this:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GObject as gobject
import pygtk
import gtk
def show_script(button):
dialog = gtk.FileChooserDialog("Open...", None, gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
dialog.set_default_response(gtk.RESPONSE_OK)
filter = gtk.FilerFilter()
filter.set_name("All files")
filter.add_pattern("*")
dialog.add_filter(filter)
response = dialog.run()
if response == gtk.RESPONSE_OK:
print (dialog.get_filename(), 'selected')
elif response == gtk.RESPONSE_CANCEL:
print ('Closed, you didnt choose any files')
dialog.destroy()
builder = Gtk.Builder()
builder.add_from_file("Wi_Green_Sheddule_v1.glade")
handlers = {
"action_show_script": show_script
}
}
builder.connect_signals(handlers)
window = builder.get_object("window")
window.show_all()
Gtk.main()
The error that my program does when i click the button is:
Traceback (most recent call last):
File "/home/pi/Downloads/showShedduleWiGreen.py", line 70, in show_script
dialog = gtk.FileChooserDialog("Open...", None, gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
File "/usr/lib/python3/dist-packages/gi/__init__.py", line 62, in __getattr__
raise AttributeError(_static_binding_error)
AttributeError: When using gi.repository you must not import static modules like "gobject". Please change all occurrences of "import gobject" to "from gi.repository import GObject". See: https://bugzilla.gnome.org/show_bug.cgi?id=709183
For starters, you are mixing up Python2 and Python3, and modules from introspection and older non-introspection modules:
from gi.repository import Gtk
from gi.repository import GObject as gobject
import pygtk
import gtk
You are importing Gtk and gtk, which are not mixable. You also don't use GObject in your code, so don't import it.
Leave just
from gi.repository import Gtk
and change all 'gtk's in your code to Gtk.
Then, take care of the indenting - else you'll still have errors. And I couldn't test anymore, as the glade file is not included...

GUI under embedded Linux / python 3.3.2

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.

Resources