Overwrite variable from another class in another file in Python - python

I'm trying to update my UI via a variable in another python file. Both are in there own class. Both saved in a folder called: System.
As I don't want to re-execute UI, I can't simply import the file.
My question: how does one change a variable from another class in another file, without re-executing?
toolsUI.py
class toolsUI:
def __init__(self):
# Store UI elements in a dictionary
self.UIElements = {}
if cmds.window("UI", exists=True):
cmds.deleteUI("UI")
self.UIElements["window"]=cmds.window("UI", width=200, height=600, title="UI")
self.createColumn() # Create Column
# Display window
cmds.showWindow(self.UIElements ["window"])
def createColumn(self):
self.UIElements["column"] = cmds.columnLayout(adj=True, rs=3)
self.UIElements["frameLayout"] = cmds.frameLayout(height=columnHeight, collapsable=False, borderVisible=True, label="To Change Label")
maintenance.py
class maintenance:
def __init__(self):
changeLabel = "Label is Changed"
self.changeLabelColumn(changeLabel) # Change Label Column
def changeLabelColumn(self, changeLabel):
import System.toolsUI as toolsUI """<--- probably not a good idea"""
cmds.frameLayout(toolsUI.UIElements["frameLayout"], edit=True, label=changeLabel)

The right way to do this afaict would be to create an object of the toolsUI type, and then operate on that instead.
import System
class maintenance:
def __init__(self):
changeLabel = "Label is Changed"
self.ui = System.toolsUI() # create a new object
self.changeLabelColumn(changeLabel)
def changeLabelColumn(self, changeLabel):
cmds.frameLayout(
self.ui.UIElements["frameLayout"], # use the object instead
edit=True,
label=changeLabel)
this way you can have multiple toolsUI objects that don't interfere with each other.

Related

Issue retrieving values from objects created by button function

I'm working on a small project and I'm having issues retrieving the values stored in combo boxes. The program has a "plus" button that creates additional boxes beneath the existing ones. They are created by calling a "create" function that makes a new instance of the ComboBox class, where the box is created and put onto the screen. A separate "submit" function is then supposed to loop through and retrieve all of the box values and store them in a list. My main flaw is that I used data in the variable names, but I have no clue how else to do this in this scenario. Does anyone have an alternative solution?
(there are some off screen variables that are show used here as parameters, but there are definitely not the source of the issue)
class ComboBox:
def __init__(self, master, counter, fields):
self.master = master
self.counter = counter
self.fields = fields
self.field_box = ttk.Combobox(width=20)
self.field_box["values"] = fields
self.field_box.grid(row=counter + 1, column=0, pady=5)
def get_value(self):
value = self.field_box.get()
return value
def create():
global entry_counter
name = "loop"+str(entry_counter-1)
name = ComboBox(window, entry_counter, fields)
values.append(name.get_value())
entry_counter += 1
def submit():
for i in range(1, entry_counter):
name = "loop" + str(entry_counter-1)
values.append(name.get_value())
For example, if I created 2 boxes and selected the options "test1" and "test2" I would want the my values list to contain ["test1, "test2"]
Not sure I understand the question right, but I guess you are asking about how to loop throw all instances of ComboBox. You can just create an global array, append new instance into it in create() method:
comboboxes = []
def create():
...
comboboxes.append(new_instance)
def submit():
for combobox in comboboxes:
...
You're on the right track with .get(). I believe your solution is that your get_value function also needs an event parameter:
def get_value(self, event):
value = self.field_box.get()
return value
See the following:
Getting the selected value from combobox in Tkinter
Retrieving and using a tkinter combobox selection

How to get reference to an instance attribute, and modify its value from outside

I'm working on an application in tkinter. I have many Entry widgets in UI, and a few classes in app engine. I need to bind tkinter variables of those entries to instances attributes.
i.e.:
class Pipe(Variable):
"""class for pipes"""
def __init__(self):
self.diameter = 0
self.variables = {}
pipe1 = Pipe(self)
pipe2 = Pipe(self)
I want to bind value from one entry to pipe1.diameter, and value from another entry to pipe2.diameter. I'm doing it by a trace function, where is lambda statement, pointing to a function, which identifies entry, and, using a dictionary proper for each instance, pass a value from entry to dictionary value. Dictionaries are produced like here, and then passed as instance attribute:
def pipe1_vars(object_):
variables = {
'ui_variable_name_for_pipe1_diameter': [object_.diameter]
}
return variables
def pipe2_vars(object_):
variables = {
'ui_variable_name_for_pipe2_diameter': [object_.diameter]
}
return variables
pipe1.variables = pipe1_vars(pipe1)
pipe2.variables = pipe2_vars(pipe2)
Unfortunately, Variable class method, assigning value, isn't working properly.
class Variable():
def set_var_value(variable_name, value):
ui_variable = tkinterbuilder.get_variable(variable_name)
self.variables[variable_name][0] = value
if ui_variable.get() != value:
ui_variable.set(value)
Obviously self.variables[variable_name][0] is something different than self.diameter. The dictionary value is changing, but instance.diameter stays the same.
How can I pass a real instance attribute to this method, instead of a copy in a dictionary value?
I'm assuming it is important to my app, to build something working as those dictionaries, because i need to bind similar attributes of different pipes to different entries - so it's have to be defined outside of a Pipe() class. I don't know if I should change dictionary to something else, or maybe should I rebuild those functions, building dictionary. I've run out of ideas, what to ask google.
Code is much complex, I've posted only most important elements, but if any other details are important, please note in comment.
If the number of Pipe attributes is small, make them properties, and when you create a Pipe object, pass it the corresponding tk binded variable:
import tkinter as tk
from tkinter import Tk, ttk
root = Tk()
var_e1 = tk.StringVar()
def print_e1():
print(var_e1.get())
def inc_e1():
var_e1.set(int(var_e1.get())+1)
class Pipe():
def __init__(self, tkvar):
self.tkvar = tkvar
tkvar.set('')
#property
def diameter(self):
return self.tkvar.get()
#diameter.setter
def diameter(self, value):
self.tkvar.set(value)
e1 = tk.Entry(root, textvariable=var_e1)
b1 = tk.Button(root, text='Print e1', command=print_e1)
b2 = tk.Button(root, text='Increment e1', command=inc_e1)
e1.pack(side=tk.LEFT)
b1.pack()
b2.pack()
p1 = Pipe(var_e1)
p1.diameter = 200
root.mainloop()

PyQt: Initialize default values for reset

I have a MainUI, that has a few buttons, line-edits and checkboxes. Most of the widgets I have set with a given state - e.g. all checkboxes are checked by default, and all line-edit fields have a default value of "1.0".
Currently I have a pushbutton called "reset everything". As its name implies, any changes made in these checkboxes or line-edit fields will be reverted to be checked and have a value of "1.0".
One way I can think of resetting the values, is by creating a function where I have to re-type the variable names of the affected widgets, along with their default state - which is similar to what I did when I created them. But I don't think this is a practical method.
My question here is: what is the best way for me to store the default values and then revert them?
So far I have created a class that contains the creation of the main ui:
class MainUI(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.setWindowTitle('UI MANAGER')
self.setModal(False)
self.init_main_ui()
self.resize(QtCore.QSize(600, 350))
# UI-Configurations
def init_main_ui(self):
self.check1 = QtGui.QCheckBox("chk_box1")
self.check2 = QtGui.QCheckBox("chk_box2")
self.check3 = QtGui.QCheckBox("chk_box3")
self.check1.setChecked(True)
self.check2.setChecked(True)
self.check3.setChecked(True)
self.max_label = QtGui.QLabel("MIN Val")
self.max_input = QtGui.QLineEdit("0.0")
self.min_label = QtGui.QLabel("MAX Val")
self.min_input = QtGui.QLineEdit("1.0")
...
The simplest approach would be to refactor your existing code so that the initial values are set by a separate method:
class MainUI(QtGui.QDialog):
...
def init_main_ui(self):
self.check1 = QtGui.QCheckBox("chk_box1")
self.check2 = QtGui.QCheckBox("chk_box2")
self.check3 = QtGui.QCheckBox("chk_box3")
self.max_label = QtGui.QLabel("MIN Val")
self.max_input = QtGui.QLineEdit()
self.min_label = QtGui.QLabel("MAX Val")
self.min_input = QtGui.QLineEdit()
...
self.reset_main_ui()
def reset_main_ui(self):
self.check1.setChecked(True)
self.check2.setChecked(True)
self.check3.setChecked(True)
self.max_input.setText("0.0")
self.min_input.setText("1.0")
...

Changing the names of tkintertable rows

So I'm working on an embedded user editable table in tkinter. I'd like to give the PlateLayout class a custom set of rows instead of the default 1,2,3...
import Tkinter as tk
from tkintertable.Tables import TableCanvas
class PlateLayout:
def __init__(self, parent):
self.parent = parent
def make_frame(self):
self.the_frame = tk.Frame(self.parent)
self.the_frame.pack()
def make_table(self):
self.the_table = TableCanvas(self.the_frame, rows=8, cols=12)
self.the_table.createTableFrame()
def make_all(self):
self.make_frame()
self.make_table()
root_win = tk.Tk()
app = PlateLayout(root_win)
app.make_all()
root_win.mainloop()
I've seen screen shots of renamed columns, but haven't found a reference as to how to do this programmatically.
This is referenced from https://code.google.com/p/tkintertable/wiki/Usage#Change_column_labels
A quick change to your code will let you set custom labels;
....
def make_table(self):
self.the_table = TableCanvas(self.the_frame, rows=8, cols=12)
# Lets peek at the current labels, delete in production
print self.the_table.model.columnlabels
self.the_table.model.columnlabels['1'] = "Custom Col"
self.the_table.createTableFrame()
....

Maya Python UI - Show an Objects ClassParent Class When Selected

Hello I need some help I'm a novice when it comes to advanced python coding, I've been trying to solve this problem but I'm unable to find an answer. I'm trying to find a way so that when someone clicks on an object inside Maya for example a basic sphere it will print the object class and parent class into each of the textFields; then should a user select something else like a cube it will instead show that object class&parent class. I know I need a function which will activate when an object is selected then place the value into the textFields, but I can't figure out how to do it. If anyone has a solution it would be greatly appreciated :)
import maya.cmds as cmds
from functools import partial
class drawUI(): #Function that will draw the entire window
#check to see if window exists
if cmds.window("UI_MainWindow", exists = True):
cmds.deleteUI("UI_MainWindow")
#create actual window
cmds.window("UI_MainWindow", title = "User Interface Test", w = 500, h = 700, mnb = False, mxb = False, sizeable = False)
cmds.columnLayout("UI_MainLayout", adjustableColumn=True)
cmds.text(label="Object's Class:")
ObjClass = cmds.textField(text = cmds.objectType,editable = False)
cmds.text(label="Object's Parent Class:")
ObjParClass = cmds.textField(editable = False)
cmds.showWindow("UI_MainWindow") #shows window
The main stumbling block is that you're using the class setup in a very unusual way. The usual pattern is to have an __init__ method that creates a new copy ("instance") of the class; in you case you're executing the code when the class is defined rather than when you invoke it. The usual class shell looks like:
class Something (object) # derive from object in python 2.7, ie, in maya
def __init__(self):
self.variable = 1 # set persistent variables for the object
do_something() # other setup runs now, when a new Something is created
def method(self):
print self.variable # you can use any variables defined in 'self'
The usual way to create a reactive UI in maya is to use a scriptJob. These will fire a script callback when certain events happen inside of Maya. The easiest event for your purpose is SelectionChanged which fires when the selection changes.
The other thing you want to do is figure out how to pack this into a class. Sometimes it's a good idea to create your class object and then give it a show() or layout() method that creates the actual UI -- a lot depends on how much you need to set the object up before showing the window.
In this case I put the behavior into the __init__ so it will create the UI when you make the object. The __init__ also creates the scriptJob and parents it to the window (so it will disappear when the window does).
class drawUI(object):
def __init__(self):
if cmds.window("UI_MainWindow", exists = True):
cmds.deleteUI("UI_MainWindow")
#create actual window
self.window = cmds.window("UI_MainWindow", title = "User Interface Test", w = 500, h = 700, mnb = False, mxb = False, sizeable = False)
cmds.columnLayout("UI_MainLayout", adjustableColumn=True)
cmds.text(label="Object's Class:")
self.ObjClass = cmds.textField(text = cmds.objectType,editable = False)
cmds.text(label="Object's Parent Class:")
self.ObjParClass = cmds.textField(editable = False)
cmds.showWindow(self.window)
cmds.scriptJob (e = ("SelectionChanged", self.update_UI), p= self.window)
def update_UI(self, *_, **__):
sel = cmds.ls(selection=True) or []
c = ["-none-", "-none-"]
if sel:
c = cmds.nodeType(sel[0], i = True)
cmds.textField(self.ObjClass, e=True, text = c[-1])
cmds.textField(self.ObjParClass, e=True, text = ", ".join(c[:-1]))
test = drawUI()
By using self.update_UI without arguments in the scriptJob, we make sure that the function that fires knows which object it's updating. Using the self variables for the textfields lets us update the UI that goes with the window without worrying about remembering names in some other scope.
More details here

Categories

Resources