I would like to create a button that I can control the look of the button using pyGTK. How would I go about doing this?
I would like to be able to point to a new image for each 'state' the button is in (i.e. Pressed, mouse over, normal...etc.)
As described in the API documentation for gtk.Image, "If you want to receive events on the image, such as button clicks, place the image inside a gtk.EventBox, then connect to the event signals on the event box.".
You probably want to use a gtk.Image rather than a gtk.Button, since buttons require more knowledge of how the theming engine works. If you really want to use a button, you'll need to read up on gtk rc files and the APIs for manipulating them.
Here's an easy way to use an image on a button. Note that there is no text given when you initialize the button (self.button1 = gtk.Button()). Adding text there would display the text instead of the image.
self.image1 = gtk.Image()
self.image1.set_from_file('images/home.png')
self.image1.show()
self.button1 = gtk.Button()
self.button1.add(self.image1)
self.button1.show()
self.backupHBox.pack_start(self.button1, True, True)
self.button1.connect("clicked", self.quit)
Related
So I have used .bind() on all my buttons and basically I would like to see which button is currently 'selected' when I switch between them with tabulator key. I did spend already quite a while on search for solution but didn't found anything useful. I don't know how to grab that moment when after pressing Tab key focus is on a button.
When there is more widgets I can .bind() Tab key to a widget proceeding button to simply just change foreground of a button but this won't work in case when I have only buttons in frame because first button will be omitted and that's not a clean and right solution to my issue.
If I bind Tab key to that button and change foreground then it's changed but after pressing Tab when button was already selected.
I don't know is there any clean solution for that problem or I will have to for frames that have only buttons create some starting_dummy widget that would initiate change for a first button.
Haven't found a way to detect is Tab key focused at this moment on a particular button (like it is with hover and cursor) but I know when it's going to be so I used that and solved case with only buttons in frame. It's based on what I already wrote in the question - I'm binding Tab key with a function to a widget preceding button to change button colour while button is bind to a function that's changing colour back to normal like this:
self.entry.bind("<Tab>", self.focus_in)
self.button.bind('<Tab>', self.focus_out)
def focus_in(self, event):
self.button.configure(fg_color='white')
def focus_out(self, event):
self.button.configure(fg_color='black')
For the case when only widgets in the frame/root are buttons, like in a menu that I got first I focus_set() on the last button and make a functions that circulate colours on and off at on Tab like this:
self.button2.focus_set()
self.button1.bind('<Tab>', self.focus_in)
self.button2.bind('<Tab>', self.focus_out)
def focus_out(self, event):
self.button1.configure(fg_color='white')
self.button2.configure(fg_color='black')
def focus_in(self, event):
self.button1.configure(fg_color='black')
self.button2.configure(fg_color='white')
Whit a custom made buttons it's just matter of replacing fg_color with a image.
If you use a button as an image holder for your logo or so, then just set state='disabled' and it will be omitted.
I am making a GUI using tkinter, I have some Button icons on my screen, that when user clicks, they execute their specific function. for example this one:
def screenshot():
# root.iconify()
myScreentshot=pyautogui.screenshot()
file_path=filedialog.asksaveasfilename(defaultextension='.png')
myScreentshot.save(file_path)
screenshot_image = tk.PhotoImage(file='images/app6.png')
screenshot = tk.Button(root,image=screenshot_image,bg='#0000CD',command=screenshot)
screenshot.place(x=640,y=500)
the problem is icons image. when I use the image as a button icon on my page, they always have a square or a rectangle on their background, although I use bg to set its color like my main page but I can still see it has a background.
How can I simply display the shape of the image without the background of the shape?
The square or rectangle is called the "relief". It is one of the visual cues that this is a button that can be pressed.
If you don't want that, set it to "flat":
screenshot = tk.Button(
root,
image=screenshot_image,
relief="flat",
bg='#0000CD',
command=screenshot
)
I would however advise you not to do this. The relief is there so that buttons are recognizable as such. Removing that in some places makes for an inconsistant user interface which can confuse new users.
I am trying to design 650pxx100px button in an application.
The rectangle contains an icon on the left and a text on the right.
In order to make everything act like a button I was considering adding a transparent button on top of the text and icon but SetTransparent has no effect and adding a mask raises an exception as SetMask does not exists for BitmapButton.
bitmap = wx.EmptyBitmap(650, 100)
button = wx.BitmapButton(panel, -1, bitmap=bitmap, pos=(100, 0), size=(650, 100), style=0)
Has anyone done something like this successfully or can suggest a course of action that would help my case?
I have worked with GTK in the past and where there is an EventBox that can contain anything and have events bind to it.
wxPython doesn't really support that. Transparency is an all or nothing affair where either everything in your app is transparent or none of it is. You can do gradients if you use the PlateButton or the AquaButton though. Or you might be able to do something if you drew the button yourself.
I want to create buttons dynamically
self.ctset = wx.BitmapButton(panel, -1, self.pic1, pos=(10,10), size=(50,50))
self.ctset.Bind(wx.EVT_BUTTON, self.add_ct)
self.ctset.SetDefault()
and the add_ct binding function
def add_ct(self, event):
pos=(10,self.yct)
self.yct+=65
self.new = wx.BitmapButton(self, -1, self.pic1, pos=pos,size=(50,50))
self.new.SetDefault()
print "Cutset"
I don't know where I am going wrong but my dynamically created buttons always seem disabled!
I want to bind a function to the dynamically created buttons that allows me to drag them around. Any ideas would be of great help!
I am pretty new to python and wxpython.
I don't see any code that captures the mouse's coordinates or even any drag-and-drop code. You need to download the wxPython demo package from the wxPython website and look at the ShapedWindow example for catching mouse coordinates. See also this old thread: http://wxpython-users.1045709.n5.nabble.com/Drag-Button-around-a-Panel-td3358640.html
In it you will find someone who is doing something very similar to what you want. I also found the following links which you might find helpful:
Drag button between panels in wxPython
How to move items smoothly in wxPython?
In Tkinter I'm trying to make it so when a command is run a widget is automatically selected, so that a one may bind events to the newly selected widget.
Basically I want it so when I press a button a text widget appears. When it appears normally one would have to click the text widget to facilitate the running of events bound to the text widget. I want that behavior to automatically happen when the user clicks the button. So that one does not have to click the button and then the text widget, but simply the button.
I'd also like it so if one started typing after the button was pressed it would automatically start filling the text widget. Again to cut out having to click on the text widget.
What bit of code does the above?
The terminology which describes what you want is "focus" -- you want to set the keyboard focus to your text widget. To do that you need to use the focus_set() and/or focus_force() methods on the text widget.