I have a treeview in the left side of an hpaned but when I try to move the bar to the left to make the treeview smaller than its automatic size instead of resizing the treeview it expands the entire program window to the right. Any ideas on how to fix this?
The relevant portions of the source are the following:
For the hpaned.
self.vpan = gtk.VPaned()
self.hpan = gtk.HPaned()
self.vpan.show()
self.hpan.show()
self.vBox1.pack_end(self.hpan, True, True, 0)
self.hpan.pack2(self.vpan,True, True)
And for the tree View.
self.ftree = gtk.TreeStore(str,str,str)
self.treefill(None, os.path.abspath(os.path.dirname(__file__)))
self.tree = gtk.TreeView(self.ftree)
self.tvcolumn = gtk.TreeViewColumn('Project')
self.tree.append_column(self.tvcolumn)
self.cellpb = gtk.CellRendererPixbuf()
self.celltxt = gtk.CellRendererText()
self.tvcolumn.pack_start(self.cellpb,False)
self.tvcolumn.pack_start(self.celltxt,True)
self.tvcolumn.set_attributes(self.cellpb, stock_id=0)
self.tvcolumn.set_attributes(self.celltxt, text=1)
self.tvcolumn.set_resizable(True)
self.hpan.pack1(self.tree,True,True)
self.tree.show()
I was going to answer this question, and then I realized that my answer was a repeat of the user's answer. But, as he hasn't posted his resolution as an answer, I will anyway. (Below is what I would have posted if he hadn't figured it out.)
Expand and Fill seem to be causing a bit of a fight for space between the various objects in this box (as I'm assuming that all of your objects do this. I used to have the habit, too, but no problem.) In the PyGTK documentation, "Expand" is defined as
True if child is to be given extra space allocated to box. The extra
space will be divided evenly between all children of box that use this
option.
and "Fill" as:
True if space given to child by the expand option is actually
allocated to child, rather than just padding it. This parameter has no
effect if expand is set to False. A child is always allocated the full
height of a gtk.HBox and the full width of a gtk.VBox. This option
affects the other dimension.
So, essentially, the box is giving everything the same space...you give more to one, it gives it to the other, and the objects all take it and get bigger. The window then has to expand to compensate.
To fix this, set one or both of those options to "False". Setting "Expand" to "False" will also shut off "Fill", but only setting "Fill" to "False" will cause the box to give the extra space to "Fill-False" objects as padding instead.
Related
I am trying to build a small software with the Tkinter module and python but I can't figure out how to set a widget size with percentages instead of pixels. I could of course do it by grabbing the size of the entire window at every moment and divide by 2 to get a size of 50%, but is there a better way to do it with the Tkinter module?
EDIT 1: To answer your questions I put my code here:
class Navbar:
def __init__(self, master):
self.maxsizeFrame = Frame(master, height = 50) #maxsize handler, prevent self.frame to grow too big
self.maxsizeFrame.pack(fill = X, expand = NO)
self.frame = Frame(self.maxsizeFrame)
self.frame.pack(side = TOP, fill = BOTH, expand = YES)
self.masteries = Button(self.frame, text = "Masteries")
self.masteries.pack(side = LEFT, fill = BOTH, expand = YES)
self.runes = Button(self.frame, text = "Runes")
self.runes.pack(side = RIGHT, fill = BOTH, expand = YES)
So I want to create a navigation bar with two button handled in "self.frame". But I want this design to be responsive, that's why I set expand to "YES". However I wanted to set a maximum size for "self.frame" but the only way I found was to pack this frame in an other one ("self.maxsizeFrame") and set expand to "NO" on this one. And finally, I would like to set the maximum expand size to half the main window, so it could be great if the height of "self.maxsizeframe" could be in percentage. Thanks for reading.
EDIT 2:
Actually it seems more efficient to build the software with the grid layout and the weight's option would be accurate.
If you would like to make it half the size of the window, use width=root.winfo_width / 2, height=winfo_height
In the maximum size. (I had to post it late due to the limit of posting per 30Mins
Ok, I wonder how no one responded yet.
So instead of sizing the component at the moment of creation, you can instead use the widget.place() method, define relwidth and relheight (0=0%,0.5=50%,1=100%,etc), this will give the widget x% of its parent width/height. Using place() you can also define relative starting positions for the widgets using relx and rely, and even apply width,height,x, and y by specifying screen units as you would do normally.
Now the fun stuff, which I don't see talked around, is that you can apply relwidth and width at the same time,by playing with negative values and joining relx and x properties, you can get really responsive sites.
Here is an example where I played a bit with this fields to get the responsiveness I've desired:(don't mind 'yposition' comes from the application context)
self.label.place(rely=yposition,x=0.025,relheight=0.04,width=150)
self.entry.place(rely=yposition,x=150,relheight=0.04,relwidth=0.95,width=-190)
self.button.place(rely=yposition,relx=0.98,x=-40,relheight=0.04,width=40)
Although it depends on the widget, you should be able to do width=30% and same with height. Can you please edit your post saying which widget you are using. I would not recommend percents though as they get funky some times.
I want to dynamically change the number of sliders on my application window, in dependence of the number of checked items in a QStandardItemModel structure.
My application window has an instance of QVBoxLayout called sliders, which I update when a button is pressed:
first removing all sliders eventually in there:
self.sliders.removeWidget(slider)
And then creating a new set.
The relevant code:
def create_sliders(self):
if len(self.sliders_list):
for sl in self.sliders_list:
self.sliders.removeWidget(sl)
self.sliders_list = []
for index in range(self.model.rowCount()):
if self.model.item(index).checkState():
slid = QSlider(Qt.Horizontal)
self.sliders.addWidget(slid)
self.sliders_list.append(slid)
The principle seems to work, however what happens is weird as the deleted sliders do not really disappear but it is as they were 'disconnected' from the underlying layout.
When created, the sliders keep their position among other elements while I resize the main window.
However, once they've been removed, they occupy a fixed position and can for instance disappear if I reduce the size of the window.
Unfortunately I'm having difficulties in linking images (it says the format is not supported when I try to link from pasteboard), so I hope this description is enough to highlight the issue.
Do I have to remove the sliders using a different procedure?
EDIT
Thanks to #eyllansec for his reply, it condenses a bunch of other replies around the topic, which I wasn't able to find as I did not know the method deleteLater() which is the key to get rid of widgets inside a QLayout.
I am marking it as my chosen (hey, it's the only one and it works, after all!), however I want to propose my own code which also works with minimal changes w.r.t. to what I proposed in the beginning.
The key point here is that I was using the metod QLayout.removeWidget(QWidget) which I was wrongly thinking, it would..er..Remove the widget! But actually what it does is (if I understood it right) remove it from the layout instance.
That is why it was still hanging in my window, although it seemed disconnected
Manual reference
Also, the proposed code is far more general than what I need, as it is a recursion over layout contents, which could in principle be both other QLayout objects or QWidgets (or even Qspacer), and be organized in a hierarchy (i.e., a QWidget QLayout within a QLayout and so on).
check this other answer
From there, the use of recursion and the series of if-then constructs.
My case is much simpler though, as I use this QVLayout instance to just place my QSliders and this will be all. So, for me, I stick to my list for now as I do not like the formalism of QLayout.TakeAt(n) and I don't need it. I was glad that the references I build in a list are absolutely fine to work with.
In the end, this is the slightly changed code that works for me in this case:
def create_sliders(self):
if len(self.sliders_list):
for sl in self.sliders_list:
sl.deleteLater()
self.sliders_list = []
for index in range(self.model.rowCount()):
if self.model.item(index).checkState():
slid = QSlider(Qt.Horizontal)
self.sliders.addWidget(slid)
self.sliders_list.append(slid)
It is not necessary to save the sliders in a list, they can be accessed through the layout where it is contained. I have implemented a function that deletes the elements within a layout. The solution is as follows:
def create_sliders(self):
self.clearLayout(self.sliders)
for index in range(self.model.rowCount()):
if self.model.item(index).checkState():
slid = QSlider(Qt.Horizontal)
self.sliders.addWidget(slid)
def clearLayout(self, layout):
if layout:
while layout.count():
item = layout.takeAt(0)
widget = item.widget()
if widget:
widget.deleteLater()
else :
self.clearLayout(item.layout())
layout.removeItem(item)
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)
This is my first post on stackoverflow. I am finally posting because I can not find this anywhere and have been searching for nearly 4 hours, but I am stuck.
Here is my code example:
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.geometry("600x100+200+200")
leftverticalFrame = Frame(root)
leftverticalFrame.pack(side=LEFT)
middleverticlFrame = Frame(root)
middleverticlFrame.pack(expand=TRUE)
rightverticalFrame = Frame(root)
rightverticalFrame.pack(side=RIGHT)
right = tk.Label(rightverticalFrame, text="Right Vertical Status Frame", bg="yellow")
right.pack(side=tk.RIGHT, fill=BOTH)
left = tk.Label(leftverticalFrame, text = "Left Vertical Navigation Frame", bg="orange")
left.pack(side=tk.LEFT, fill=BOTH)
bottom = tk.Label(middleverticlFrame, text="Middle Vertical Frame", bg="blue")
bottom.pack(side=tk.BOTTOM, expand=True, fill=tk.BOTH)
root.mainloop()
What I am doing is merely trying to layout the frames individually within the root because the frames will use different managers. The left frame is functioning exactly as I want it to, as is the middle frame. The problem is with the frame on the right.
Notice when you re-size the window making it more narrow, the right frame comes into the "middle frame's territory". Now the strange thing is the middle frame does not replicate the same behavior when it comes to the boundary of the left frame. I want the right frame to behave the same as the middle frame. Essentially I am trying to make the Left and Right fairly static, but the middle frame more dynamic. Can anyone tell me what I am doing wrong please?
An important thing to remember about pack is that the side attribute doesn't refer to the side of the window, it refers to the side of the remaining available space. The causes the order in which you pack things and the side that you pack them to be significant, because each time you pack something you change the location and amount of remaining available space.
In this case, the problem is that you didn't specify the side attribute for the middle frame, so it defaults to "top" (as in, "top of the remaining space", not "top of the window"). Since there's already something on the left, this puts it at the top of the remaining space on the right. Then, when you put the next item on the right, it's on the right but below the thing that is on the top.
There are at least a couple ways to solve this. The first is to pack the left and right sides first, and then pack the middle. In this case it doesn't matter which side you put the middle frame:
leftverticalFrame.pack(side=LEFT)
rightverticalFrame.pack(side=RIGHT)
middleverticlFrame.pack(expand=TRUE, side=TOP)
The second solution is to leave them in the original order, but pack the middle frame on the left or right instead of the top:
leftverticalFrame.pack(side=LEFT)
middleverticlFrame.pack(expand=TRUE, side=LEFT)
rightverticalFrame.pack(side=RIGHT)
These two variations will initially look identical, or perhaps nearly identical depending on what else might be in the frames or in the window. However, the behavior is different when you start to make the window too small to fit all of the frames.
In such a case, tkinter must eventually start reducing the size of a widget. It does this in the reverse order that they were packed (read: the last one to be packed is the first one to be shrunk). That means that if you want the left and right to be fixed as much as possible, you should pack the middle section last.
pro tip: it makes your code easier to read and maintain if you group all of your layout code together. Consider this code:
f1 = Frame(...)
f1.pack(...)
f2 = Frame(...)
f2.pack(...)
I think you'll find over time that your code is easier to read and maintain if you write it like this:
f1 = Frame(...)
f2 = Frame(...)
...
f1.pack(...)
f2.pack(...)
...
I think it makes the code much easier to visualize, since all of the layout for a given parent window is in one place rather than sprinkled throughout the code.
I have a label that I want right aligned and the text to be right aligned. But when my code runs through and the label updates it, the StaticText aligns to the left of a button object. My code is below
hbox14 = wx.BoxSizer(wx.HORIZONTAL)
self.buttonRemove = wx.Button(self.panel,label='Remove')
self.buttonRemove.Bind(wx.EVT_BUTTON,self.removeAccount) # Remove account from list
self.labelSecTic = wx.StaticText(self.panel,label='0.0',style=wx.TE_RIGHT|wx.EXPAND)
self.labelSecTic.SetForegroundColour('white')
self.labelSecTic.SetBackgroundColour('black')
hbox14.Add(self.buttonRemove,proportion=0)
hbox14.Add(self.labelSecTic,proportion=1,flag=wx.ALIGN_RIGHT|wx.TE_RIGHT|wx.EXPAND)
When the label is updated I call
self.gui.labelSecTic.SetLabel(str(self.diff))
Any suggestions on how to make the labelSecTic stay fixed to the right side of the panel? Thanks.
First a side note: the style wx.TE_RIGHT is for wx.TextCtrl, it probably does nothing with the static text. About your real issue, you should force layout of the hbox14 sizer. Not sure what is the sizer/panel structure of your window, you should call Layout on some ancestor of hbox14, it might be self.gui.panel or even self.gui (don't know what gui is), so for example:
self.gui.labelSecTic.SetLabel(str(self.diff))
self.gui.Layout()
or
self.gui.labelSecTic.SetLabel(str(self.diff))
self.gui.panel.Layout()