Is there a way of drawing non-canvas lines in Tkinter? - python

Hi !
I've been looking all over StackExchange and some other forums for an answer to my question but I couldn't find anything relevant, so here I am, posting my question.
What I'm trying to do is to draw lines that are on the same layer as other Tkinter widgets. I'm currently coding an interface where certain widgets need to visually connect to each other (NodeBox style or Quartz Composer style).
Shows the connectors (noodles) present in Quartz Composer. (I'm not necessarily looking for curved lines, straight is more than enough.)
The problem is that it is too complicated to use the canvas widget (I will be using a lot of widgets so embedding all of them into the canvas is not really an option I think). I'm looking for something similar to the separator widget but allowing for diagonal lines and defined by coordinates. I'm thinking of creating a custom widget that does this but I'm not sure where to start. An other solution may be to have a transparent canvas right over the non-canvas widgets but that would complicate the mouse click events a lot. I don't know what option would be the best.
Any thoughts on how I could accomplish drawing lines out of canvas widget? (Or on how I can create a custom widget that does this?)

Your only reasonable choice for drawing lines is the canvas. Without the canvas you can simulate horizontal or vertical lines using a separator and place, but you can't do diagonal lines.

Related

2d graphics in Python Kivy, advice is needed

I still have been studying world of Kivy and I have encountered with question. I am designing an interface. it has to have an appearance of analog device with arrow and curved scale like old voltmeters were. Just to display a data on the screen. I've tried to use canvas, but I stacked with resizing of window properties. Built canvas object was either connected to specific size in px, or changed location on the screen in wrong way. So, I am curious, maybe here are some others ways to embed objects which were built in another 2d graphics library, I am aware of existence of 3d module vpython and turtle. Maybe we have some more?
Can anybody give me a hint? I would be very grateful
kivy, canvas
view of my prototype
You can use Image widgets combined with Button Behavior or ToggleButton Behavior to make buttons that look like almost any kind of button that you like. And you can make a curved scale as described in this question.

Creating a "Fill" command in tk inter

I am pretty new to Python and coding in general. I have been working on a program that is similar in nature to ms paint. So far, I've added the capabilities to create multi-colored rectangles, lines, ovals, and really any polygon.
I've been using the tkinter GUI. I've been wanting to add a fill command, but I'm kind of stuck as to how to start it. My idea for how it would work would be that it would check the color of the pixel the user is currently hovering over, then check up, down, left, and right for the same color in pixels. If it found that, it would change the color of those pixels (I guess by creating a really small rectangle object?). This would theoretically be able to fill an area. But, I really can't find anything on how to access the color of a pixel in tkinter.
I know the location is event.x and event.y for a specific event, but I can't find anything about pixel color. I don't really have any code written for it yet because I am unsure that tkinter can even access the color of a pixel and not just object colors.
Unfortunately, this isn't possible. I did some searching around, and found several other similar questions, but the general idea is that Tkinter does not support such a feature. It makes sense, considering that Tkinter is a GUI library.
I saw a suggestion somewhere, where an idea was proposed to create 1x1 rectangles using the Tkinter Canvas to basically mimic pixels. However, this method eventually leads into performance issues and lagging, so it's not really recommended either.
You may want to try exploring some other libraries to work together with Tkinter. You can keep the Tkinter GUI, but use an image manipulation library or something similar which integrates well with Tkinter, for the actual pixel drawing.

Checkers board in kivy

What it is the best way to make a chessboard for checkers using Kivy framework?
I have board.png, white.png, black.png, white_q.png, black_q.png files already. I wonder how to assign to each black tile on my board.png its own coordinate. Should I create 32 transparent widgets placed on black tiles of board.png or it is impossible? And what widget to use for 24 checkers? Any ideas or it is too complicated using Kivy and I should use tkinter?
There are many ways you could do this. It isn't complicated, it's very easy. The best way depends more on how you want to structure your app than anything else.
I wonder how to assign to each black tile on my board.png its own coordinate
Set the pos attribute of a widget to control its position, or better in this case use a layout that does what you want. For instance, adding your squares to a GridLayout with the right number of columns will have the right effect without you needing to worry more about positioning them.
Should I create 32 transparent widgets placed on black tiles of board.png or it is impossible?
I don't understand what you're asking here. You can make transparent widgets if you want but I don't know why you'd want to.
And what widget to use for 24 checkers?
The real question is, what do you want the widget to do? e.g. if you want it to display an image then inherit from Image.
Overall this answer is very generic because your question is very generic. I suggest that if you're stuck, try to ask a more specific question about a task you're struggling with, and give a code example showing where you are now.

How to drag objects around in a Gtk.Fixed widget using mouse events?

I have a custom object(a small circle) placed at some point inside a Gtk.Fixed() widget .Is there a way to drag this object around using the mouse. I am not able to map the Mouse Press/Release/Motion events to make this work.
I would prefer solution in Python using PyGobject but any other language will do also be fine if explanation is provided
More Details:
I am trying to make a font editor where these objects I mentioned above will be the control points of the bezier curves in the Glyph outlines
Here is an image of the concept design:
https://github.com/sugarlabs/edit-fonts-activity/blob/gh-pages/files/img/wireframe_concept_01_first_prototype.svg
I need to able to move the points shown to edit the outline of the letter shown
GtkFixed is not designed to do drawing work. It's made to locate widgets (such as buttons and such) on a fixed grid (รก la Windows).
If you would like to move elements of a drawing, have a look at eg. GooCanvas. Each element on a goocanvas can have events connected, which can then be used to move it around. You can even use CanvasGroup to group primitives (circle, rectangle etc), and move them together (even change other properties such as color, linewidth). The toolbox actually contains curves etc. It's easy to create a 'handle' using a small rectangle.
Here's an example of a simple goocanvas program, and you can find download links, references manuals and other useful stuff here.
I don't know if this is a tool you need, or just a learning exercise. If the former, then do have a look at FontForge, an open source font editor, and incredibly versatile.

wxPython : Adjusting the panels and sizers for different screen resolutions

So, here I'm happy that I wrote the whole code for a awesome looking GUI using wxPython in a day but it evaporated when I found that the panels are getting out of the way leaving a lot of empty space on the sides or getting congested (you know how!) on a different screen resolution.
What I want to ask is that what all properties of a GUI should I adjust or care about if I want to see that the GUI's aspect ratio, frame alignment, panel alignments, sizer ratios etc. should remain intact or if there're any methods to do so, suggest me.
Thanks in advance. :)
Sizers automatically adjust your application widgets for screen resolution, resize, etc. If they aren't doing this automatically, then there's probably something buggy in your code. Since I don't have your code to look at, try going over the wxPython tutorials on sizers very carefully. I found the book wxPython In Action very useful on this topic.

Categories