I am trying to add Maccept method to the ImageDialog class and connect this to okButton. However when I compile this code it will give :
AttributeError: 'ImageDialog' object has no attribute 'Maccept'
But I have already defined Maccept method inside the class.
from PyQt5.QtWidgets import QDialog,QApplication
from ui_imagedialog import Ui_ImageDialog
import sys
class ImageDialog(QDialog):
def __init__(self):
super(ImageDialog, self).__init__()
# Set up the user interface from Designer.
self.ui = Ui_ImageDialog()
self.ui.setupUi(self)
# Make some local modifications.
#self.ui.colorDepthCombo.addItem("2 colors (1 bit per pixel)")
# Connect up the buttons.
self.ui.okButton.clicked.connect(self.Maccept())
self.ui.cancelButton.clicked.connect(self.reject)
def Maccept(self):
print 'accept'
def main():
app=QApplication(sys.argv)
window=ImageDialog()
window.show()
sys.exit(app.exec_())
if __name__=='__main__':
main()
it was a stupid mistake. i am using vi as an editor and it has auto indent feature apparently puts a tab character for the indentation. I replaced with 4xspace character and solved.
Related
I am trying to figure out what instance variables do not show up in auto completion in an IPython or Jupyter notebook. For example, if I have the class below:
class A:
def __init__(self, var_a: int):
self.var_a = var_a
and I define a class B that takes an instance of A at initialization, it seems like autocompletion does not look through to the definition of class A to see that it has a var_a instance attribute.
class B:
def __init__(self, var_b: A):
self.var_b = var_b
def func(self):
self.var_b.
I do not get any type hints if I press tab after the last period in the snippet above. Pylance (through the VS Code Python extension) and PyCharm both show autocompletion for self.var_b.var_a. Is there a way to configure either IPython or the code annotations so that autocompletion will also work in IPython?
Using IPython 8.4.0
Tried %config Completer.use_jedi = True in the first notebook cell which did not help.
Explicitly annotating the namespaces of the classes should work:
class A:
var_a: int
def __init__(self, var_a: int) -> None:
self.var_a = var_a
class B:
var_b: A
def __init__(self, var_b: A) -> None:
self.var_b = var_b
It doesn't get more explicit than that. If this code doesn't lead to proper type inference/auto-suggestions, that should be considered a bug.
I'm trying to break a tkinter app with several classes into multiple .py files. I'm using a Mixin class to import methods into each class. However, I'm struggling to access variables.
main.py contains a class creating the main window with a button to open a top level window and a button to get a variable from the Mixin.
# main.py
import tkinter
from tkinter import Tk
import customtkinter
import sys
sys.path.insert(1, "path/Classes")
from Classes import topLevel
# Main Window
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
# Top Level Button
self.topLevel_button = customtkinter.CTkButton(master=self,
text="Open",
command=self.openTopLevel)
self.topLevel_button.grid(row=0, column=0)
# Get Variable
self.get_button = customtkinter.CTkButton(master=self,
text="Get Variable",
command=self.getVariable)
self.get_button.grid(row=1, column=0)
def openTopLevel(self):
window = topLevel.topLevel(self)
def getVariable(self):
print(var) # Can't access var
if __name__ == "__main__":
app = App()
app.mainloop()
topLevel.py is a class creating the top level window. It contains a variable fileSep to be used by a method in the Mixin Class:
# topLevel.py
sys.path.insert(1, "path/Methods")
from Methods import _topLevel
class topLevel(customtkinter.CTkToplevel, _topLevel.Mixin):
def __init__(self, parent):
super().__init__(parent)
# Create Variable
global fileSep
fileSep = customtkinter.StringVar(value="Comma")
print("Inside Class: " + fileSep.get()) # Works
# Create Button
self.loadButton = customtkinter.CTkButton(master=self,
text="Load",
command=self.loadFile)
self.loadButton.grid(row=0, column = 0, sticky='nswe')
# Attempt to access variable
def access_method(self):
print("Access Method: " + self.fileSep.get())
And _topLevel.py contains the mixin class:
# _topLevel.py
class Mixin:
def loadFile(self):
# Trying to access fileSep variable
print("Inside Mixin: " + fileSep.get()) # Error
topLevel().access_method() # Error
# I'm trying to access this variable from a function in main.py
var = "Hello World"
I get the following errors because the variables aren't accessible.
NameError: name 'var' is not defined
NameError: name 'fileSep' is not defined
I've tried making variables global as well as creating methods inside the class ( access_method() ) to print the variables as described here https://www.geeksforgeeks.org/python-using-variable-outside-and-inside-the-class-and-method/, but get the error:
TypeError: __init__() missing 1 required positional argument: 'parent'
How do I access variables that are defined in a class in the Mixin class? How would I access a variable created by the loadFile function in the Mixin Class for use in methods in the class App?
Thanks to #acw1668 comment, I just needed to make fileSep an instance variable:
self.fileSep = customtkinter.StringVar(value="Comma")
And to access it in the mixin class:
print("Inside Mixin: " + self.fileSep.get())
Equally I could create an instance variable to assign variable var to. For example in the topLevel class I added:
self.a = ""
And then assigned var to this variable within the function:
var = "Hello World"
self.a = var
I wondered if anyone could tell me is it possible to divide the following code into two python classes which will then be placed in two different files:
from GUI import mainGUI
from GUI import subwindowGUI
class MyApp(QMainWindow, mainGUI.Ui_MainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
# mainGUI contains QMdiarea in which it opens
# a subwindow by calling a function MySubWin(self)
# QMdiarea name is 'body'
self.OpenSub.triggered.connect(self.MySubWin)
def MySubWin(self):
self.MySubWin = QWidget()
self.MySubWin.setObjectName('Module window')
self.myTabs = QtabWidget(self.MySubWin)
self.myTabs.setObjectName('myTabs')
# now I call the function that will display
# the gui elements inside this 'Module window'
self.create_module_gui()
self.body.addSubWindow(self.MySubWin)
self.MySubWin.showNormal()
def create_module_gui(self, *args):
module_gui = subwindowGUI.Ui_ModuleWindow()
module_gui.setupUi(module_gui)
self.myTabs.addTab(module_gui, _('New tab'))
self.myTabs.setCurrentWidget(module_gui)
As you can see from the code above my functions are interacting with the main window gui elements. I wanted to move these functions that are related to this specific module into a separate file for the maintaining purposes. That's why I'm asking you to help me on how to achieve that if it's even possible. Thanks in advance, Tomislav.
#Mel:
If I for move those functions into another file:
myFunctions.py
class MyFunctions(object):
def MySubWin(self):
self.MySubWin = QWidget()
self.MySubWin.setObjectName('Module window')
self.myTabs = QtabWidget(self.MySubWin)
self.myTabs.setObjectName('myTabs')
# now I call the function that will display
# the gui elements inside this 'Module window'
self.create_module_gui()
self.body.addSubWindow(self.MySubWin)
self.MySubWin.showNormal()
def create_module_gui(self, *args):
module_gui = subwindowGUI.Ui_ModuleWindow()
module_gui.setupUi(module_gui)
self.myTabs.addTab(module_gui, _('New tab'))
self.myTabs.setCurrentWidget(module_gui)
and then I import that file into my main file.
import myFunctions
class MyApp(QMainWindow, mainGUI.Ui_MainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
# mainGUI contains QMdiarea in which it opens
# a subwindow by calling a function MySubWin(self)
# QMdiarea name is 'body'
self.OpenSub.triggered.connect(MyFunctions.MySubWin)
What I end up with is the error saying that MyFunctions object has no attribute body.
1st I must say that you won't gain any maintenance out of this ... but for example lets move the create_module_gui to another class
in a new file: creategui.py
class CreateGUI(object):
def create_module_gui(self, *args):
module_gui = subwindowGUI.Ui_ModuleWindow()
module_gui.setupUi(module_gui)
self.myTabs.addTab(module_gui, _('New tab'))
self.myTabs.setCurrentWidget(module_gui)
in your code:
from GUI import mainGUI
from GUI import subwindowGUI
from creategui import CreateGUI
class MyApp(QMainWindow, mainGUI.Ui_MainWindow, CreateGUI):
#yay you have your create_module_gui method now...
Now you can also just put create_module_gui as a function in another file and call it with self as the first param...
See this solution: https://stackoverflow.com/a/47562412/10155767
In your case, don't make a class in myFunctions.py. Instead, define the functions at the top level. Thus myFunctions.py should look like:
def MySubWin(self):
...
def create_module_gui(self, *args):
...
Then in your original file, import the functions within the class like
class MyApp(QMainWindow, mainGUI.Ui_MainWindow):
from myFunctions import MySubWin, create_module_gui
def __init__(self, parent=None):
...
I'm new to python and I have a hard time trying to figuring out how can I inherit from a class in an other module.
module: ~/foo.py
import bar
class foo:
def test(self)
print("this is a test")
module: ~/bar.py
class bar(foo):
def __init__(self):
super().test()
As soon as bar is imported, I get this error message :
NameError: name 'foo' is not defined
If you want to refer to a name in another module then you must import it.
import foo
class bar(foo.foo):
...
I have a list of Symbols representing packages, objects and classes and want to import them in a macro context.
For packages and objects, this would mean a wildcard import and for classes it would mean a “standard” import.
Given a List[Symbol] consisting of some.package, some.Class and some.Object, how would I properly import those and how can I decide whether a “standard” or a wildcard import needs to be used?
My current approach is this:
def importPackageOrModuleOrClass(sym: Symbol): Import =
if (sym.isPackage || sym.isModule) // e. g. import scala._, scala.Predef
gen.mkWildcardImport(sym)
else // e. g. import java.lang.String
gen.mkImport(sym.enclosingPackage, sym.name, sym.name) // <--- ?????
The package/module import works, but the class import doesn't although it looks correct.
You need to get the "TermName" like this...
def importPackageOrModuleOrClass(sym: Symbol): Import =
if (sym.isPackage || sym.isModule)
gen.mkWildcardImport(sym)
else
gen.mkImport(sym.enclosingPackage, sym.name.toTermName, sym.name.toTermName)
You can grab more hints regarding importing, reflecting, etc. via the sourcecode at http://xuwei-k.github.io/scala-compiler-sxr/scala-compiler-2.10.0/scala/reflect/internal/Importers.scala.html