**Python 3.5.1
Is it possible for a label beside a text box in a row then another label beside another text box in another row & the labels should be aligned as well as the text boxes?
I already tried it with the grid manager and it worked but my professor wanted us to use the pack manager and I don't know how to align them.
Expected Output //Using Pack manager but I created this with the grid manager
I think it had got to do with the sequence of my packages and the anchor & side. When i used the side='top' to manipulate the 1st label so that the 2nd label below it should be aligned, the 1st text box can't be aligned by row on the 1st label. But when i will use either anchor='n' or 'ne', 1st label & 1st text box worked fine but the 2nd label stayed in line with the 1st one.
Here is the current code i'm working at (I can't paste it on the code sample)
Thank you for helping!
( I already got the answer thanks for the help! I used frames for controlling the labels & textboxes.)
Related
I'm having issues with vertical alignment of texts when using the svgwrite python library.
For example, here's a simple code that generates a red-filled circle with a black text saying "Text" on top of the circle:
import svgwrite
d = svgwrite.Drawing(filename='alignment_test.svg',
size=(60,60))
circle = d.circle((30,30), 30, fill='red')
text = d.text('Test', (30,30),
style='text-anchor:middle',
font_size='17px')
d.add(circle)
d.add(text)
d.save()
The result is this expected image:
However, I want the text to also be vertically aligned.
I tried using the alignment-baseline attribute of SVG, i.e.
style='text-anchor:middle;\
alignment-baseline:middle'
However, it doesn't work - neither in FireFox (where the vertical alignment just doesn't work), not in Inkscape, which complains Unimplemented style property 363.
I would appreciate any suggestions on how to solve the matter.
...so while writing this question I searched around a bit more, and actually read the second answer here (by toutankh):
vertical alignment of text element in SVG,
which suggests using the attribute dominant-baseline instead of alignment-baseline for text objects. And it works perfectly.
Lesson learned: never read only the first answer to a SO question.
So I'm new to tkinter, but I've got what I want working, up to a certain point.
I'm not sure I've set it up correctly, but I've got a world map with buttons on the right, and an events log on the left, which fills up with labels as stuff happens.
Issue is that after a little while, the whole log fills up.
What is the best way to delete all the labels, or maybe delete the oldest (top) label each time?
Here's what I mean:
Defined here:
root=Tk()
Map=PhotoImage(file="C:/Users/Willam/Desktop/CWProgram/map2.gif")
background=Label(root,image=Map).place(x=100,y=0,relwidth=1,relheight=1)
Title=Label(root,text=' LOG').pack(anchor=NW)
And I create my labels like this:
info=Label(root,text='Select a sector to move units from',wraplength=170)
info.pack(anchor=NW)
I tried the usual info.destoy() and info.forget(), but these only work on the last label used in that function.
Should I have grouped all labels or something?
As PM 2Ring suggested it is usually useful to append labels to a list for future ref:
tmp = Label(...)
labels.append(tmp)
then just:
foreach label in labels: label.destroy()
If you do not want a list, and you're sure you want to clear everything in root:
foreach label in root.children.values(): label.destroy()
The children dict always holds the objects contained within. If you want to keep the map label, you will have to make your own list as I showed, without appending info into it.
I would recommend using:
info.pack_forget()
For each pack you created you must do it in the format:
packname.pack_forget()
Which if you have a lot of packs is impractical, but otherwise it works very well.
This also makes it very easy to selectively remove some labels and leave others as it will not purge all packs that you placed.
Just use:
root.children.clear
After clearing screen just input map and functions again...
I am trying to list all the categories placed left of each other. The problem I am facing is that it is going beyond the frame boundaries and not coming down to the new line. Any work around for this ?
var = IntVar()
for i in xrange(len(ultraCategories)):
i = Radiobutton(midFrame,text=ultraCategories[i],variable=var,value=i,command=sel)
i.pack(side = LEFT)
If you use the .grid layout manager for Tkinter you can specify the row and column of where you would like to place each item. There is no built in function to split items in to a new row.
Tkinter Grid Layout
I'm developing some tool in PyQT4 and Python 2.7 and I stuck in a little problem. I've 3 buttons in stored in widget and that widget is in cell in the table (QTableWidget). So my problem is that I can't align widget to top of cell and tool doesn't resize row height to defined in widget minimum/fixed height.
Here is how it looks now with method resizeRowToContents.
And I want something like this
I did something similar with fixed row height but its not the way I want.
I solved this problem in quite simply way.
First of all I changed margin of layout to 0px (later I changed that to 3px to have padding) and this made my alignment exactly to top, left corner. Next step was set minimum size for each row and that I made by following line:
self.verticalHeader().setMinimumSectionSize(30)
added in constructor.
After that I received exactly what I want:
widget with some pading from top and bottom
not fixed size of rows so multiline text in item is allowed as well
I try to put sixteen checkbuttons into frame, placing them into four columns like:
c1 = Tkinter.Checkbutton(group.interior(), text = 'Name', indicatoron= 1, variable = self.Checkvar_nr, command=cb)
c1.(row = 0, column = 0)
and so on up to:
c16.(row = 3, column = 3)
Everything's fine except columns vertical alignment because of the differences in the length of the text used.
How to align then horizontally?
I don't quite understand the problem, since columns must be vertically aligned since it's a grid. I think what you're saying is that the items in each column aren't aligned to a column boundary. Try using sticky='w' when adding each checkbutton to the grid. This will cause them to "stick" to the left edge of the column.
As an option, try placing each element in a non-stretchable graphic element.
The thought is that the layout manager is maximizing the use of the screen real estate. Because you want to take up more space, which is contrary to the layout manager's algorithm, you will need to find a graphic container that doesn't "change size".
Sometimes you can do this through manually editing the text string (less preferred). Other times you can use a table like structure (HTML for instance). Other times you can use a frame with defined width and height attributes. These frames are then placed inside of the columns as elements.
Note: It's been a long time since I played with Tk. I'm going by memory. Best of luck!
(edit:) Going from memory, the columns will adjust their width based on content. If there are several three character labels in the first column and five character labels in the second column, the width of the two columns will be different. (Note: This will be depended on the layout manager.) If there is a 'fixed width' option for the layout manager in question, then it should keep all column widths the same.
With layout managers that rearrange with dimensions based on content (HTML, CSS, etc), it is sometimes necessary to place the content inside "immovable" containers. Usually these are frames. The frames work as bounding boxes. This approach works when the element that needs to have a width and height does not have that feature.