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):
...
Related
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 originally made scripts with many functions on 2 individual scala worksheets. i got them working and now want to tie these individual scripts together by importing and using them into a third file. from what i have read you can not simply import external scripts you must first make them into a class and put them into a package. so i tried that but i still couldn't import it
i know this may be a bit basic for this site but im struggling to find much scala documentation.
i think my problem might span from a missunderstanding of how packages work. the picture below might help.
my program example
adder.scala
package adder
class adder {
def add_to_this(AA:Int):Int={
var BB = AA + 1;
return BB
}
}
build.scala
package builder
class build {
def make_numbers(){
var a = 0;
var b = 0;}
}
main.sc
import adder
import builder
object main {
adder.adder.add_to_this(10);
}
the errors i get are
object is not a member of package adder
object is not a member of package builder
Classes in scala slightly differ from classes in java. If you need something like singleton, you'll want to use object instead of class i.e.:
package com.example
object Main extends App {
object Hide{
object Adder{
def addToThis(AA:Int):Int = AA + 1
}
}
object Example{
import com.example.Main.Hide.Adder
def run(): Unit = println(Adder.addToThis(10))
}
Example.run()
}
Consider objects like packages/modules which are also regular values. You can import an object by its full path, i.e. com.example.Main.Hide.Adder you can also import contents of an object by adding .{addToThis}, or import anything from object by adding ._ after an object.
Note that classes, traits and case classes could not be used as objects, you can't do anything with it unless you have an instance - there are no static modifier.
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.
I am trying to initialize a derived class from text file input. A simple example of what I am trying to do:
file.txt:
1
2
main.py:
class Base:
def __init__(self, val1):
self.val1 = val1
def input_from_text(cls, init_deque):
#return cls(init_deque.popleft())
class Derived(Base):
def __init__(self, val1, val2):
Base.__init__(self, val1)
self.val2 = val2
def input_from_text(cls, init_deque):
#initialize base and derived here and return derived
def main(argv=None):
initialized_derived = Derived.input_from_text(deque(open("file.txt")))
assert initialized_derived.val1 is 1
assert initialized_derived.val2 is 2
Is there a good way to do this? Basically looking for something similar to what you would find in C++ with:
//calls operator>>(Base) then operator>>(Derived)
cin >> initialized_derived;
This way each class is nicely encapsulated and the base/derived classes don't need to know anything about each other (excepting __init__ which knows the number of args base takes).
Just realized that I was going about this the wrong way. Simple fix is to do something like:
class Base:
def __init__(self):
pass
def input_from_text(self, init_deque):
self.val1 = init_deque.popleft()
class Derived(Base):
def __init__(self):
Base.__init__(self)
def input_from_text(self, init_deque):
Base.input_from_text(self, init_deque)
self.val2 = init_deque.popleft()
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):
...