I am trying to create a roguelike using the Text widget.
I have figured out a few things, namely that I can set the size of the widget using width and height options and that I can find the pixel height or width of said widget. However, what I want to do is have the widget resizable (pack(expand="yes", fill="both")) but be able to refresh the displayed text on a resize. Is there a way to get the character dimensions when the widget is running without resorting to winfo_width() and math based on pixel dimensions of characters?
I've run into that exact same problem a couple times jaccarmac, and to my knowledge there is no way to find the width of a string of characters. Really the only way is to use the winfo_ commands: width, height, geometry. However, it kind of sounds like you just want to make sure that all of the text is displayed if you change the label and add more text. If that is the case, you don't have to worry about it. That should all be taken care of by the widgets themselves. If you don't see them expanding to show all of your label, that usually means one of the widgets containing that label is not set to expand (either using expand=YES with .pack, or columnconfigure(i, weight=1) for .grid).
A final thought; in the pack arguments make sure it's YES, and not "yes". That uppercase YES is not a string, but a variable name defined by Tkinter.
There is no way to automatically get the width in characters, but it's easy to calculate, assuming you're using a fixed width font. One way to do this is to use the font_measure method of a font object. Use font_measure to get the width of a '0' (or any other character for that matter; I think tk users zero internally, not that it matters with a fixed width font), then use this in your calculations.
This is an old question, but after doing some research I've found that there actually is a way to get height/width info directly without maths or playing with font widths using the Text widget's cget() method:
text_widget = tk.Text()
width_in_char = text_widget.cget('width')
height_in_char = text_wdiget.cget('height')
Since the Text widget stores its height and width configuration in characters, simply querying for those parameters will give you what you're looking for.
Related
colors = Label(
text="Surprise",
foreground="Bisque",
background="Hot Pink",
width=150,
height=100
)
colors.pack()
Pycharm showing me to remove the attribute height it is not an option
There are a few things to note here. First: in order to make a label, you need to specify which window it should go in, so the first parameter will normally be root. Second: the height option is for the number of lines of text not the number of pixels, this setting you are trying to use will be very very big (same for width). Normally you shouldn't try to set the size of a label in this way because in most cases it will just automatically go to the correct size for the text you put inside of it. Instead, if you want a specific number of pixels, you should create a Frame widget with the specific size and put the Label widget inside of the frame widget
I'm trying to make a little GUI script using tk. After computation on the basis of user input, I generate a simple string that I want to print using a listbox, but it was suffering from alignment problems.
I tried printing the output in the console at the same time to check whether this was a formatting error:
for loop :
string = foo(x)
listbox.insert(END, string)
print string
The problem is that the console is using a fixed width font but the listbox is using a variable width font. In a variable width font, characters like "i" (lowercase I) and "l" (lowercase L) take up less horizontal space that characters like "M" and "0".
If you want characters to line up in a listbox like they do in the console, you need to use a fixed width font. You can configure the font used by the listbox via the font attribute.
Tkinter provides several default fonts, the default fixed-width font is named "TkFixedFont". This default font will be approximately the same vertical height as the default variable width font that is used by other widgets. The exact font that is chosen may be different on different platforms, but is typically a variant of courier.
For example:
import Tkinter as tk
root = tk.Tk()
listbox = tk.Listbox(root, font="TkFixedFont")
If you wish to be explicit about the font family and size, you can provide that as a string, tuple, or as a font object. For example, picking a courier font of size 18 could be specified as font="Courier 18".
listbox = tk.Listbox(root, font="Courier 18")
For more information on fonts, see the TkDocs tutorial on fonts, colors and images and the section Widget Styling on effbot.
Is it possible to get the size/extents of rendered text in reportLab?
I need to calculate some positions relative to other elements on the page.
The flowable system isn't want I'm looking for. I don't mind using it to get the sizes if necessary, but it's not appropriate for my overall layout. I tried creating a Paragraph an calling wrap but it returns the available width always, not the measured extends.
You may get the width of a rendered text with stringWidth:
from reportlab.pdfbase.pdfmetrics import stringWidth
width = stringWidth(text, font_name, font_size)
Line spacing gives you the height of a line of text.
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.
I have been tasked with inserting and re-sizing several hundred images to a powerpoint.
I need to use a particular source format that is similar to other power points used by our company.
I have been playing around with activepython's win32com API's and I have figured out how to open up a file and create a blank slide.
My question is how would I go about inserting an image and resizing it to whatever size I need(the images will be the only thing on each page). Also I am trying to use my company's theme for the background and title page but this is not as important as getting the images on page and re-sized.
any help would be greatly appreciated, thanks!
I got this from the page Xavier referred to:
Pict1 = Slide1.Shapes.AddPicture(FileName=pictName, LinkToFile=False, SaveWithDocument=True, Left=100, Top=100, Width=200, Height=200)
That will work if your original images are square; otherwise, it will distort them.
Better to specify -1 for the width and height. Then PPT will insert them at their "natural" size (whatever PPT might decide that is ... not important for present purposes). Then you can read the shape's size to determine its aspect ratio and make sure that stays constant if you change the size or you can set the shape's .LockAspectRatio property to true and adjust either height or width and the other will auto adjust to maintain the aspect ratio.
You should use Shapes.AddPicture as described here (archived version of this broken link):
from tkinter import *
import tkinter.filedialog as tkFileDialog
import win32com.client # middleman/translator/messanger between windows and python
import win32com.gen_py.MSO as MSO # contains constants refering to Microsoft Office Objects
import win32com.gen_py.MSPPT as MSPPT # contains constants refering to Microsoft Office Power Point Objects
g = globals() # a dictonary of global vlaues, that will be the constants of the two previous imports
for c in dir(MSO.constants): g[c] = getattr(MSO.constants, c) # globally define these
for c in dir(MSPPT.constants): g[c] = getattr(MSPPT.constants, c)
Application = win32com.client.Dispatch("PowerPoint.Application")
Application.Visible = True # shows what's happening, not required, but helpful for now
Presentation = Application.Presentations.Add() # adds a new presentation
Slide1 = Presentation.Slides.Add(1, ppLayoutBlank) # new slide, at beginning
TenptStr = Slide1.Shapes.AddShape(msoShape10pointStar, 100, 100, 200, 200)
pictName = tkFileDialog.askopenfilename(title="Please Select the Image you wish to load")
print(pictName)
Pict1 = Slide1.Shapes.AddPicture(FileName=pictName, LinkToFile=False, SaveWithDocument=True, Left=100, Top=100, Width=200, Height=200)
Note that the "choose file" option produces a file path with '/" rather than '' and you may need to replace the former by the latter.
Firstly I would resize them first before insertion using PIL python imaging library. In general mucking about with any office image processing has been uniformly awful for me so I would not recommend it.
secondly the object model examples in VBA found in PowerPoint help or online are usually all you need to get a long way
from memory the insert was pretty easy -
ppt.slide.ImageFromFile("path")
however exact positioning I do not remember and I am afraid I don't have ppt here to try out. If I get achance later I will post some references
Good luck with the docs
I agree with the accepted answer. I would like to point out that I needed to prefix the FileName string with an 'r' to treat the backslashes as literal characters. Escaping the backslashes by replacing single backslashes with double also works. #vestland this may be helpful