I was wondering whether it is possible to use some of the tkinter canvas drawing methods on a text widget. Ideally I would have the text widget placed onto the canvas so that I can draw onto the canvas and make it look like it shows up on the text widget.
No, it is not possible to draw over or into a tkinter Text widget. You can, however, add text to a canvas with the create_text method and draw over that.
Related
How can I create buttons and labels in a canvas widget and make the whole set of widgets in the canvas scrollable?
I came across a _create() option in my IDE but I'm not sure how to use it. I tried entering Button for itemType but it didn't work.
I tried create_window() but I'm not sure how to use it either.
My question is not how to use a loop and add the buttons, but how can I create widgets and place them in a canvas, but scrollable? Also is there a _create()? If yes, is there any way to create widgets using it?
How to create buttons and labels in a canvas widget and make the whole set of widgets in the canvas scrollable?
You have two choices:
use the canvas method create_window to add the buttons and labels to the canvas at specific coordinates
use the canvas method create_window to add a frame to the canvas, and then put your buttons and labels inside of the frame using pack or grid or place.
You then have to make sure you configure the scrollregion of the canvas to be large enough to include the widgets.
I came across a _create() option in my IDE but I'm not sure how to use it.
The following example shows how to use create_window to add a button to a canvas. The procedure is the same for any type of widget.
canvas = tk.Canvas(root)
canvas.pack(fill="both", expand=True)
the_button = tk.Button(canvas, text="Click me", ...)
canvas.create_window(10, 10, window=the_button, anchor="nw")
canvas.configure(scrollregion=canvas.bbox("all"))
Since Text(Tk(), image="somepicture.png") is not an option on text boxes, I was wondering how I could make bg= a .png image. Or any other method of allowing a text box to stay a text box, with an image in the background so it can blend into a its surroundings.
You cannot use an image as a background in a text widget.
The best you can do is to create a canvas, place an image on the canvas, and then create a text item on top of that. Text items are editable, but you would have to write a lot of bindings, and you wouldn't have nearly as many features as the text widget. In short, it would be a lot of work.
So in tkinter I'm making a program, I want to know can you make a title box thing like the one in this image with the title "Booking Details"?
Sounds like you want a Label Frame.
The LabelFrame widget is a variant of the Tkinter Frame widget. By default, it draws a border around its child widgets, and it can also display a title.
What the title says.
I'm having a problem moving the textbox from a side to side.
The code's long and it's about 200+ lines so I wont post it here.
Anybody has an idea?
You have various options for this, depending on what you mean by "text box," and whether you want to move it "by pixels" or "from a side to [another] side."
If you just want to display text, you can use a Label widget. If you want a text box where the user can enter text, try an Entry widget. If you want to move your widget from one area of the screen to another, you can use the grid geometry manager and simply use grid_forget to "unplace" your widget then grid (with different options than you originally used, of course) to put it somewhere else.
If you just have text and you'd like to move it pixel by pixel, you could create a Canvas and then use that widget's create_text method to create some text in a specific place on the Canvas. You can the use the Canvas widget's itemconfig method to move the text to a new location.
If you need something more complex than text, like an Entry widget, and you want to move it pixel by pixel, do the same as above but use the create_window method instead.
See Canvas, grid, Label, Entry, and these SO questions about create_window.
I'm using Python Tkinter and I want to place a variable number of text box widgets in a frame or canvas. The text boxes are packed vertically down the frame, so the first one is on top, the second one is found below, etc.. I can have all the button, listbox, etc widgets in a "left section" of the GUI, while a "right section" will only contain the text box widgets. I want the text box widgets to horizontally expand when the master window is maximized, but because there's a variable number of these widgets, the "right section" containing the text boxes also needs to be able to vertically scroll to view them all.
Currently, I'm using Canvas.create_window to add my variable number of text boxes to the canvas, and while I can scroll the canvas to view all the text boxes, they do not horizontally expand when I resize the window. I have an alternate GUI that uses a frame for the "right section", which allows the widgets to horizontally expand, but if too many are packed, I cannot scroll the frame to see the additional text boxes because I can't have a scroll bar tied to a frame.
Is there any way around this trade-off?
The solution is pretty simple: bind to the <Configure> event of the canvas -- this will cause your callback to be called whenever the canvas widget is resized. You then simply need to get the width of the canvas and use that to iteratively resize all the embedded windows.