The following bit of code populates a class that inherits from the wx.Menu class, with menu items. So self is basically a wx.Menu. When I run the flowing code snippet, the line about.SetBitmap(wx.Bitmap('Icon24.ico')) seems to change the highlighting from the default:
to this plain blue style:
The line about.SetBitmap(wx.Bitmap('Icon24.ico')) simply adds an icon (test). But, for some reason it changes the highlighting style. I know this is about as nitpicky as it gets, but I'd like to the first images highlighting style with the second images Icon. This is in Windows 7 if it makes a difference.
Snippet:
about = wx.MenuItem(self, -1, 'About...')
about.SetBitmap(wx.Bitmap('Icon24.ico')) # The line that's causing the problem.
itemlist = [self.AppendItem(about),
self.AppendSeparator(),
self.Append(-1, 'Options...'),
self.AppendSeparator(),
self.Append(-1, 'Exit')]
for i in itemlist:
self.Bind(wx.EVT_MENU, self.menu_beh, i)
Well, according to the WxWidgets doc, the version 2.8 doesn't support Windows Vista or 7, so I think this is expected since looks like it reset the entire menu theme to the XP style, I think the best you can try is to use the testing version 2.9, which support Vista (but not 7).
Related
I writing gtk interface with python code. the problem is:
init entry widget by gtkbuilder:
self.some_entry = self.builder.get_object('SomeEntry')
Define signal by typing button, after then must changed entry color:
def on_SomeButton_clicked(self, widget):
self.some_entry.modify_bg(Gtk.StateType.NORMAL,Gdk.Color(20000,10000,10000))
but it doesn't work, such 'modify_base'. And I don't know why. Help please.
Sorry for my English(
EDIT2:
Turns out it was an entry box giving the problem which is another issue in and of itself because the background is not the property you need to modify but the BASE color property which can typically be set using:
self.entry.override_background_color(Gtk.StateType.Normal, Gdk.RGBA(0.0, 1.0, 0.0, 1.0))
However for OP it was not working so a CSS option was explored, listed at: https://mail.gnome.org/archives/gtk-app-devel-list/2005-November/msg00236.html
EDIT:
So working with PyGtk3 I was able to get a button changing color using you line of code:
self.button.modify_bg(Gtk.StateType.Normal, Gdk.Color(20000, 10000, 10000))
It was grey on initialization and dark red after the code ran. The only thing I could think of is make sure that the object you are trying to modify is actually in a NORMAL state after you run the code and make sure the signal you think is triggering is actually triggering.
==============
Original post:
Without having the full code here there are a few things that could be causing this. I just threw together a test program in Python based off of: http://pygtk.org/pygtk2tutorial/examples/helloworld.py
When I set the Gtk State for the modify_bg I had to use:
gtk.STATE_NORMAL
Not sure if that is due to a different version of Gtk or what though.
Then when I went to use Gdk I had to refer to it as:
gtk.gdk
The line that I ended up with to change the button color was:
self.button.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(20000, 10000, 10000))
Hopefully that works out, in order to get any more detailed though we would definitely need more code and to know what kind of errors you are getting.
CSS works, code below:
style_provider = Gtk.CssProvider()
css = open('style.css', 'rb')
css_data = css.read()
css.close()
style_provider.load_from_data(css_data)
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
With line
"widgetname".get_style_context().add_class("colorize")
"widgetname" has been colorized.
CSS code:
.colorize {
background: rgba(200,50,50,0.3);
}
I'm creating a GUI with PyQt4 for my bachelor thesis. I have a QComboBox, where every item is a checkbox. Here is my code:
somewhere in the constructor:
self.multi = QtGui.QComboBox(self)
self.multi.setEnabled(True)
self.multi.view().pressed.connect(self.handleItemPressed)
and here I fill the QComboBox with my checkboxes:
def fillMultiCombo(self):
# len(self.featureNames) rows, 1 column
model = QtGui.QStandardItemModel(len(self.featureNames), 1)
firstItem = QtGui.QStandardItem("feature(s)")
firstItem.setBackground(QtGui.QBrush(QtGui.QColor(200, 200, 200)))
firstItem.setSelectable(False)
model.setItem(0, 0, firstItem)
for i,query in enumerate(self.featureNames):
item = QtGui.QStandardItem(query)
item.setFlags(QtCore.Qt.ItemIsEnabled)
item.setData(QtCore.Qt.Unchecked, QtCore.Qt.CheckStateRole)
model.setItem(i+1, 0, item)
self.multi.setModel(model)
Just to say in advance: I think it's not a code issue, but I provided some code to make it clearer.
The problem now is: On Windows 7 (on 2 different machines) it all works fine. But on my tutor's machine (Macbook Pro, I don't know which OS sorry), the checkboxes are not showing (but no error or warning printed), BUT when you click it, the checkbox is checked. So it's like the checkbox is there and functions, but it is invisible.
So is this a bug, machine dependent or some other issue. Because all other things work on her machine totally smooth.
After a long time I've finally found the problem. Here is the important code snippet:
app = QtGui.QApplication(sys.argv)
# Available styles (depending on the OS) for my ThinkPad:
# Windows
# WindowsXP
# WindowsVista
# Motif
# CDE
# Plastique
# Cleanlooks
# makes the split bar visible
app.setStyle(QtGui.QStyleFactory.create('Plastique'))
So before I set the style for my application, Qt chose the most appropriate style depending on the plattform resp. OS. So the default style Qt chose for my machine worked and the checkboxes are normally shown. The default style Qt chose for the application on the machine from my tutor was not the same. In fact if I change from 'Plastique' to 'Cleanlooks' my checkboxes also disappear.
First of all, it is important to mention that I'm learning Python and Gtk+ 3, so I'm not an advanced programmer in these languages.
I'm trying to make a graphical interface in Gtk3 for a Python script that creates a png image, and I'd like to display it, but the PyGobject documentation is so scarce that I haven't found a way to do that. So far, my interface looks like this:
The buttons and text entries are arranged in a grid, and I'd like to keep empty the big space (represented by the big button) to the right until the script finishes building the image, and then show it in that area. The code is here.
Is there a way to do that using Python in Gtk3?
Thanks in advance,
Germán.
EDIT
Taking a look at the demos pointed out by #gpoo I discovered the Frame widget, and I implemented it in my GUI. This is how it looks like:
Inside the window class, I add the Frame to the grid:
self.frame_rgb = Gtk.Frame(label='RGB image')
self.frame_rgb.set_label_align(0.5, 0.5)
self.frame_rgb.set_shadow_type(Gtk.ShadowType.IN)
self.grid.attach_next_to(self.frame_rgb, self.label_img_name,
Gtk.PositionType.RIGHT, 3, 8)
I also connect the Run button to a callback function, so that when I click on it, my script creates and then displays the png image:
self.button_run = Gtk.Button(stock=Gtk.STOCK_EXECUTE)
self.button_run.connect('clicked', self.on_button_run_clicked)
self.grid.attach_next_to(self.button_run, self.entry_b_img,
Gtk.PositionType.BOTTOM, 1, 1)
Finally, my callback function is (no calculations yet, only render the image to the Frame for testing purposes):
def on_button_run_clicked(self, widget):
self.img = Gtk.Image.new_from_file('astro-tux.png')
self.frame_rgb.add(self.img)
but I got the following error when I click the Run button:
(makeRGB.py:2613): Gtk-WARNING **: Attempting to add a widget with
type GtkImage to a GtkFrame, but as a GtkBin subclass a GtkFrame can
only contain one widget at a time; it already contains a widget of
type GtkImage
Any help is appreciated!
You can use Gtk.Image. If you generate a file, you could use:
img = Gtk.Image.new_from_file('/path/to/my_file.png')
and add img to the container (GtkGrid in your case). Or, if you already have the Gtk.Image there, you can use:
img.set_from_file('/path/to/my_file.png')
Instead of ...from_file you can use from_pixbuf, and you can create a Gdk.Pixbuf from a stream.
In general, you can use the documentation for C and change the idiom to Python. Also, you can check the demos available in PyGObject, in particular, the demo for handling images.
The following code sample works under linux (ubuntu) and Windows XP, but not under OSX.
import wx
class frame(wx.Frame):
def __init__(self,p=None):
wx.Frame.__init__(self,p)
self.box = wx.ListBox(self)
self.box.AppendItems( ["Zero","One","Two","Three","Four","Five","Six"])
self.box.Bind(wx.EVT_MOUSE_EVENTS,self.onMouse)
def onMouse(self,evt):
pos = evt.GetPosition()
print self.box.HitTest(pos)
evt.Skip()
class guiApp(wx.App):
def __init__(self,redirect=False):
wx.App.__init__(self,redirect)
def OnInit(self):
f = frame()
f.Show()
self.SetTopWindow(f)
return True
if __name__=="__main__":
app = guiApp()
app.MainLoop()
On Linux and Windows, the correct items are identified when moused over. On OSX hittest always returns -1 (wx.NOT_FOUND)
I'm running 32-bit wxPython, 2.8.12.1 (mac-unicode) which uses the Carbon API in 32bit python 2.7.2.
I can't find this listed as a known bug in wxWidgets and I'm hesitant to submit as it seems this should work. The listbox control is deeply integrated into out GUI and I really don't want to swap it out for ListCtrl or something similar as we have all other functionality working now. Does anyone know a workaround?
There is no work around if the listbox is scrolling. The scroll is handled be the underlying Carbon library and scroll position is not accurately reported back through wx.
I found the bug in the wxWidgets source code and opened a ticket on the wxWidgets trac, http://trac.wxwidgets.org/ticket/13699, with a patch.
The root of the bug is a call to the Mac's underlying DataBrowser with an incorrect rowId argument. wxWidgets was passing row position offsets, assuming this would be the rowId's (and maybe at some point apple used these internally when true Id's weren't specified.) Adding a call to another function translates a row's position (offset) into it's real id. With a patched version of wxWidgets the above script works as expected.
I need to get focus to a specified window, and the only way I'm seeing on my head, is minimizing all windows on front of it until I get the right one...
How can I do it?
Windows 7, and no specific toolkit....
Every type of window, for example, firefox and console command
You'll need to enumerate through the windows and match the title of the window to get the one you want. The code below searches for a window with "firefox" in the title and sets the focus:
import win32gui
toplist = []
winlist = []
def enum_callback(hwnd, results):
winlist.append((hwnd, win32gui.GetWindowText(hwnd)))
win32gui.EnumWindows(enum_callback, toplist)
firefox = [(hwnd, title) for hwnd, title in winlist if 'firefox' in title.lower()]
# just grab the first window that matches
firefox = firefox[0]
# use the window handle to set focus
win32gui.SetForegroundWindow(firefox[0])
To minimize the window, the following line:
import win32con
win32gui.ShowWindow(firefox[0], win32con.SW_MINIMIZE)
You'll need to enumerate through the windows and match the title of the window to get the one you want. The code below searches for a window with "firefox" in the title and sets the focus
To minimize the window use the following line:
def enumHandler(hwnd, lParam):
if 'firefox' in win32gui.GetWindowText(hwnd):
win32gui.ShowWindow(hwnd, win32con.SW_MINIMIZE)
win32gui.EnumWindows(enumHandler, None)
This works for Windows 10, Python3.5 32bit, pywin32‑223.
I reported the above case, but an error occurred.
Traceback (most recent call last):
TypeError: The object is not a PyHANDLE object
I'm assuming from the question, that you want to write a generic to that can work with any window from any application.
You might want to try the Python Win32 GUI Automation library. I haven't used it but sounds like it might be what you are looking for. If that doesn't work, your best option might be forgo python and use a tool like AutoIt that provides built in support for window manipulation.
If neither of those solutions work you will probable have to directly invoke windows api. I do not know if the win32api package wraps the necessary functionality, otherwise you will have write a python module in c/c++.
If this kind of functionality is available in the .net api, you could use IronPython.