In python notebook Change the height of a ipywidget - python

I am using ipywidgets in jupyterlab for displaying later on with voila.
In order to display information I create several text on containers
wd_EXTRA_output = widgets.Output(layout={'border': '1px solid black'})
with wd_EXTRA_output:
print('Information will be displayed here... ')
I dont want to pass a 'height' at the moment of creating the Output widget.
How can I later on modify the height of that widget?
wd_EXTRA_output(layout={'height': '200px'})
Note: I know I could have passed the height at the moment of creating the widget, but for particular reasons I cant do that.

When you directly change the python attributes of a widget the display will update automatically. So in this case you can do:
wd_EXTRA_output.layout.height = '200px'
you can also set the height to other valid html heights, e.g. '50%'

Related

Adjusting opacity and updating the properties of a text object in a canvas in Python

I created a canvas and inside I put an image. I also can change the properties of the watermark text and would like to update the text on the canvas when I make some changes.
I created the text inside of the canvas with create_text and when I initialize the program, I created a variable. text_variable = canvas.create_text(...) However, I couldn't also adjust the opacity of the text.
Problems:
I can't adjust the opacity
I can't update color, font style, font size and position after I put this text on the canvas.
I expect:
Change the text when I change some properties from the edit menu
Add opacity adjustment to the text
self.watermark_display = self.display_canvas.create_text(self.watermark_start_position_x, self.watermark_start_position_y, text="Plese write your watermark!", font=(self.fonttype.get(),self.fontsize.get()),fill=self.color_choice)
def update_watermark_display(self):
self.display_canvas.itemconfig(self.watermark_display, self.watermark_start_position_x, self.watermark_start_position_y, text="Plese write your watermark!", font=(self.fonttype.get(),self.fontsize.get()),fill=self.color_choice)
When I try to do with this way, I got Type Error.
self.display_canvas.itemconfig(self.watermark_display, self.watermark_start_position_x, self.watermark_start_position_y, text="Plese write your watermark!", font=(self.fonttype.get(),self.fontsize.get()),fill=self.color_choice)
TypeError: Canvas.itemconfigure() takes from 2 to 3 positional arguments but 4 were given
You cannot use itemconfig to change the coordinates. Instead, use the coords method for the coordinates, and itemconfig for the item configuration.
self.display_canvas.itemconfig(
self.watermark_display,
text="Plese write your watermark!",
font=(self.fonttype.get(),self.fontsize.get()),
fill=self.color_choice
)
self.display_canvas.coords(
self.watermark_display,
self.watermark_start_position_x,
self.watermark_start_position_y
)

ipywidget interactive hiding visibility

I would like to make an interactive module with ipywidgets.
So far so good but I'm stuck.
I want to hide the visibility of a certain ipywidget object dependent on a certain situation, and I want my printed text to show up above the widget and stay there.
dropdown=widgets.Dropdown(
options={'Coffee machine': 1, 'Washing machine': 2, 'Water Heater': 3, 'Heating System': 4, 'Dryer': 5, 'Oven': 6, 'Microwave': 7, 'Other':8},
value=1,
description='Apparaat:',
)
text_new=widgets.Text()
def text_field(value):
if(value==8):
display(text_new)
text_new.on_submit(handle_submit)
else:
text_new.visible(False) #Doesn't work but I want something like this
print("Today you had an increase in electricity consumption, would you like to name this device?") #This just be above the dropdown menu and be stuck
i=widgets.interactive(text_field, value=dropdown)
display(i)
What this does now:
When "Other" is checked in the dropdown menu, a text box appears where the user can type something.
However, when checking another machine, the text box stays there.
I just need a "hide" function but I can't seem to find one that works.
Also, after checking another option on the dropdown, the print dissapears, not coming back.
Had same problem so i found in
boton.layout.visibility = 'hidden'
or
check.layout.display = 'none'
they made some changes... i got if from here
Cannot create a widget whose initial state is visible=False
Given a widget:
import ipywidgets
button = ipywidgets.Button()
There are two direct ways to hide the the widget, with a notable difference.
Hide and unhide the widget without affecting overall page layout:
# Turn the widget "invisible" without affecting layout
button.layout.visibility = "hidden"
# Make the widget visible again, layout unaffected
button.layout.visibility = "visible"
Hide and unhide the widget and collapse the space that the widget took up:
# Hide widget and collapse empty space
button.layout.display = "none"
# Re-add the widget, adjusting page layout as necessary.
button.layout.display = "block"
When to use each one? As a rule of thumb, use layout.visibility so the page layout is not constantly jumping around as visibility is toggled. However, for very large widgets, consider using layout.display to avoid huge blank spaces.
For more general CSS information that applies here, see What is the difference between visibility:hidden and display:none?
In addition to the accepted answer, if you want to dynamically change the visibility of a control, you can declare the layout variable and reuse.
layout_hidden = widgets.Layout(visibility = 'hidden')
layout_visible = widgets.Layout(visibility = 'visible')
Like attach to an event:
def visible_txt(b):
text_box.layout = layout_visible
def hidden_txt(b):
text_box.layout = layout_hidden
btn_visible.on_click(visible_txt)
btn_hidden.on_click(hidden_txt)

Maya HUD troubles

I create script for playblast. I need some HUD data over my video like a user and scene name, fps and current frame...
First i try HUD created by headsUpDisplay() is good, but not have a background... I change color of HUD labels but sometimes they are not readable without a background.
cmds.headsUpDisplay('HUDObjectSceneName', label='label TEXT',
section=2, block=0, blockSize='large',
dfs='large', labelFontSize='large')
Second i try use HUD buttons created by hudButton() - they have a background. But one of my label - is current time. headsUpDisplay() have 'command' to refresh and change label text. But hudButton() does not have this functionality.
label = 'FPS: 25 FRAME:'
cmds.hudButton('HUDHelloButton3', s=9, b=0, vis=1, l=label,
bw=blockLen(label), lfs='large')
cmds.headsUpDisplay('HUDCurentFrame', label=label,
section=9, block=0, blockSize='large', dfs='large',
labelFontSize='large', atr=True,
command=lambda: cmds.currentTime(query=True))
hudButton() have second trouble - width of button is set manually. and when i want long label i need to calculate label width. but HUD font is not fixed and i don't know how right calculate a label width in pixels. After some experiments i create this function to calculate width. It made rough, but at least as that:
def blockLen(label):
FONT_WIDTH = 8
THIN_WIDTH = 6
BLOCK_ADD = 10
thin_symbol = ' :,.!i[];:\'"|-'
sum = BLOCK_ADD
for x in label:
sum += THIN_WIDTH if x in thin_symbol else FONT_WIDTH
return sum
I need HUD label with background and dynamic data like a current frame. But i can't find another way how create it?
ps. I try to use scriptJob() to change HUD button label when time changed. But its not worked with playblast...
scriptJobs do not execute when animations are playing. If you really need to update the hud during playback you can trigger your update from inside an expression. You'll have to call it from mel, unfortunately. And keep it as light as possible, it will slowdown interactive playback for anybody viewing the animation.
You might want to dynamically create the expression before playblasts and then delete it right afterwards so you don't leave it lying around to bother your animators.
You can also get out of using HUD buttons by creating an image plane set to an appropriate color.
One part of my problem i decided to. I don't find how to update button directly. I create headsUpDisplay() without label - he is able to updated. And i forced him to change the text on my hudButton()
def frame_label():
label = 'FPS: 24 FRAME: %s' % cmds.currentTime(query=True)
cmds.hudButton('HUDCurentFrame', e=True, l=label)
# bottom-right: FPS and current frame info
cmds.headsUpDisplay('HUDCurentFrameInvisible', label='',
section=9, block=1, blockSize='large', dfs='large',
labelFontSize='large', command=frame_label, atr=True)
cmds.hudButton('HUDCurentFrame', s=9, b=0, vis=1, l='', bw=200, lfs='large')
But second part of my problem not solved. I cant calculate text size in pixels. The correct solution is to get from the Maya which font is used for HUD. And then i can use wx library to calculate width of text using font name...
But how to get font data (name, size and decorations) from Maya?
to your second problem:
i was able to find the needed font data (only name, size) but its not really accurate(more hacking, no voting needed), if you change the view port renderer to ViewPort 2.0 and than
and changing the sizes of the font you will get the Error (nor on the default renderer):
# small display ui font size and display ui size
cmds.displayPref(sfs=9, dfs=10) #font size
cmds.savePref()
Failed trying to load font: -*-helvetica-bold-normal-*-9-*-*-*-*-*-iso8859-1
so the used font is helvetica bold and the size is relativ(you own input or the default value like cmds.optionVar(q="defaultFontSize"))

How to hide ttk Treeitem indicators in a Python GUI

I'm building a Python GUI application using ttk treeviews. On Linux, when a Treeitem has child items, it displays an arrow to show that the row can be expanded. I want to hide this indicator arrow (I am using other ways to hint that the row can be expanded). How can I do this?
If I run Style().element_names() I see that there's a Treeview element and a Treeitem.indicator element. If I run Style().configure("Treeview", padding=50), I see the padding style get applied when I create the treeview, so I feel confident that any style I correctly apply to Treeitem.indicator should be visible also.
Running Style().element_options("Treeitem.indicator"), I see ('-foreground', '-indicatorsize', '-indicatormargins'). Running Style().lookup("Treeitem.indicator", "foreground") gives me "#000000", so it appears that value is initialized. If I try Style().configure("Treeview", foreground="#123456") I don't see the indicator arrow change color, though running Style.lookup("Treeitem.indicator", "foreground") shows me "#123456" as expected. My plan was to set the indicatorsize to 0 to make the arrow go away entirely, but I cannot even successfully edit the color. What am I doing wrong here and is there a better way to hide the indicator? In case it matters, I'm running Python 3.5.0.
Not sure if you ever figured it out.
When you create the new style and configure it, you have to change the name of the template to ".". This changes the root style for the treeview. You also need to specify a theme, even if it is default. So it would look something like:
s = ttk.Style()
s.configure(".", indicatorsize = '0')
s.theme_use('default')
Then when you create the treeview, you shouldn't have to specify a style at all.
Let me know if this works for you.
Edit: Since this is being downvoted for some reason, I'll clarify:
Code with the style part commented out:
#s = ttk.Style()
#s.configure(".", indicatorsize = '0')
#s.theme_use('clam')
j = ttk.Treeview(self.parent)
j.place(relx = 0.5, rely = 0.5, anchor = "center")
j.insert("",1,"jacob",text = "Jacob")
j.insert("jacob",1,"marcus",text = "Marcus")
j.insert("jacob",2,"tony",text = "Tony")
j.insert("jacob",3,"ricardo",text = "Ricardo")
gives us
Code with the style part present
s = ttk.Style()
s.configure(".", indicatorsize = '0')
s.theme_use('clam')
j = ttk.Treeview(self.parent)
j.place(relx = 0.5, rely = 0.5, anchor = "center")
j.insert("",1,"jacob",text = "Jacob")
j.insert("jacob",1,"marcus",text = "Marcus")
j.insert("jacob",2,"tony",text = "Tony")
j.insert("jacob",3,"ricardo",text = "Ricardo")
Hope this helps.
EDIT 2:
Added the s.theme_use('clam') line because you need to specify which theme you're using. It also works with classic and default, but for some reason doesn't work with the vista theme.

Updating cell widgets changes in a Qtable without selecting the cell

well I'm using Pyqt4 in Maya2012 to make a reference editor alike ui. I working with a QtableWidget to make the reference list and i have subwidgets in each cell. One of the widgets is a checkbox that unload or reload the reference.
The problem i have is if a click directly in the checkbox without have the cell selected it doesn't do anything
this is my code:
def listConnections(self):
self.pos=self.sender().currentRow()
wid = self.list.ref_l.cellWidget(self.pos, 0).children()
self.text = self.list.list[self.pos]
self.ref()
for wt in wid:
if type(wt)== type(QCheckBox()):
wt.stateChanged.connect(self.changeState)
if type(wt)== type(QComboBox()):
wt.currentIndexChanged.connect(self.changeType)
I'm calling the function with a "itemSlectionChanged" signal because is the only way i knew i could detect the subwidgets.
All the subwidgets are made in the moment i fill the list.
Is there a way to make what i want?
Edit:
This is how i called the function
self.list.ref_l.itemSelectionChanged.connect(self.listConnections)
and this is how i create all the subwidgets in the cells
def fillList(self):
mayaRef = self.findRef()
if len(mayaRef)>0:
for count in range(0,len(mayaRef)):
self.ref_l.insertRow(count)
wid=QWidget()
cLayout=QHBoxLayout()
wid.setLayout(cLayout)
checkWid=QCheckBox()
nameWid=QLabel()
cLayout.addWidget(nameWid)
nameWid2=QLabel()
cLayout.addWidget(nameWid2)
comWid=QComboBox()
cLayout.addWidget(comWid)
self.ref_l.setCellWidget(count,0,wid)
self.ref_l is my QTable Widget, this is in another code that i'm calling with self.list in the original
You should set up all your connections right after you create the check boxes in fillList. Each item is associated with the path of the reference. You can use a QSignalMapper to map each check box to the path, then connect the signal mapper to your changeState slot. The signal mapper then calls that slot with the path you specified.

Categories

Resources