wxpython toolbar not shown in os x - python

I have a python script for GUI using wxpython. It works perfectly fine in Windows, however, when I run the script in OS X, the toolbar is now shown (I installed wxpython from its official website and used the cocoa version, and I am using OS X 10.10 and python 2.7). Following is the part of the code regarding the toolbar:
self.toolBar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL|wx.TB_FLAT|wx.TB_DOCKABLE)
self.myChoice = ComboBoxWithHelp(self.toolBar, wx.NewId(), size=(200, -1), value=..., choices=..., style=wx.CB_DROPDOWN,)
self.toolBar.AddControl(self.myChoice)
iconname = 'icons/new.png'
self.toolBar.AddSimpleTool(1, wx.Image(iconname, wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'New', 'New')
...
self.toolBar.Realize()
self.SetToolBar(self.toolBar)
Nothing is shown below the menu bar, however the space is left there. Did I installed the wxpython wrongly or use the function wrongly?
By the way, the above code also works for Ubuntu.

What is self in this code? Toolbars are a little different on OSX, and and can be a bit tricky, so there may be some issues if self is not a wx.Frame or a class derived from wx.Frame. This is because the native toolbars are actually part of the frame rather than an independent widget. It should be switching to a non-native toolbar if the parent is not the frame, but then you'll need to manage its size and layout yourself.
If self is already the frame then you may want to try not specifying the style flags and let it just use the default style, or you can try creating the toolbar like this instead and let the frame create it:
self.toolBar = self.CreateToolBar()

Related

Program in wxPython and VPython

I wrote program in wxPython and VPython, but I have huge problem: Module VPython not always run. If I run program in Interpreter, program run more often , than after compilation. Code never returns any errors. I try on the other computers, program is the same.
Win7
Python 2.7.5 32bit
wxPython 2.9.4
VPython 6.0.5
import visual as v
import wx
class Maintest(v.window):
def InitUI(self):
fileMenu = wx.Menu()
self.menubar.Append(fileMenu, '&Plik')
self.menubar.Remove(0)
def asd(self, scene):
v.box()
def main():
w = Maintest(menus=True, title="program", x=100, y=100, width=1024, height=600)
w.InitUI()
scene = v.display(window=w, x=0, y=0, width=600, height=600, up=(0,0,1), background=(1,1,1), foreground = (0,1,0), forward=(-1,-1,-1))
w.asd(scene)
while True:
v.rate(60)
if __name__ == '__main__':
main()
All is OK: http://imageshack.us/photo/my-images/199/tpp.png/
Not OK: http://imageshack.us/photo/my-images/689/akun.png/
It is only the part of main program, but problem is the same.
What is wrong? What I should to do?
I will be gratefull for answer.
With Win7 Python 2.7.5 64bit wxPython 2.9.4 VPython 6.0.5 I cannot get the program to fail. The fact that the problem is not entirely reproducible (and is entirely nonreproducible by me) suggests the possibility that the graphics driver needs to be updated. VPython uses the OpenGL 3D graphics library, which is sensitive to driver issues.
Only a guess but you may have let something out:
import wx
app = wx.App(redirect=True)
top = wx.Frame(None, title="Hello World", size=(300,200))
top.Show() # you need this
app.MainLoop() # you need this
I had a similar issue, or at least one with similar symptoms, and found that it was dependant on the position of the mouse at launch time (see full description here).
The solution was to call Hide and Show in succession after the display was created.
These calls need to be made to the panel fed to the display object.
win.panel.Hide()
win.panel.Show()

After adding "primary toolbar" style class to a gtk.Builder object, gtk.ToolButtons in toolbar are not primary-themed

I've been trying to get my toolbar to look like the standard toolbars in stock Ubuntu apps, and it works to an extent, except the buttons in the toolbar do not have the correct theming applied to them. The text is colored for the default toolbar, and on hover the buttons are an ugly tan color instead of getting darker grey as they do in the standard Ubuntu apps.
In other words I'm getting this:
Instead of this:
(I don't have enough rep to post images directly)
I've been using the following method to change the toolbar style, since Glade doesn't seem to have an option to change it itself:
self.bd = Gtk.Builder()
self.bd.add_from_file("builderfile.glade")
self.bd.connect_signals(self)
...
toolb = self.bd.get_object("toolbar")
toolb_style = toolb.get_style_context()
toolb_style.add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
This works to the extent that the toolbar is now darkened, but the buttons are incorrectly themed. I tried adding buttons to the toolbar manually after setting the STYLE_CLASS_PRIMARY_TOOLBAR instead of using Gtk.Builder() but they weren't the right color either.
I suppose manually creating a toolbar and filling out all the information might work, but it would be nice to be able to use Glade for the design.
I'm writing this answer here since it's a solution, but it's not ideal because it doesn't use Glade and it's kind of messy. I'll have to wait until a new build of Glade gets into Ubuntu, since apparently the feature has been added upstream.
The solution I eventually used was just to do all the toolbar creation in standard GTK and python, and then add it to an empty Gtk.Box defined in Glade. Like this:
bd = Gtk.Builder()
bd.add_from_file("gladefile.glade")
...
button1 = Gtk.ToolButton(stock_id=Gtk.STOCK_ADD, ...)
button2 = Gtk.ToolButton(...)
toolbar = Gtk.Toolbar()
toolbar.get_style_context().add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
toolbar.insert(button1, 0)
toolbar.insert(button2, 1)
toolbar.show_all()
bd.get_object("toolbar_slot").pack_start(toolbar, False, True, 0)

How can I get consistent behavior on my wxPython StyledTextControl?

I am having a problem with wxPython. I am attempting to have a scrollable window without a visible scroll bar. I still want to be able to use the mouse wheel to scroll as well as use the keyboard shortcuts that I have written.
I have the following simplified code:
import wx
import wx.stc
app = wx.App(0)
frame = wx.Frame(None, wx.ID_ANY, "Sample Scroll pane")
textViewer = wx.stc.StyledTextCtrl(frame, wx.ID_ANY)
textViewer.Text = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22"
textViewer.SetUseVerticalScrollBar(False)
textViewer.ScrollLines(1)
frame.Show()
app.MainLoop()
I am using the "ScrollLines" function to scroll my text programatically. On a windows machine this works as expected and scrolls down one line. However, on Ubuntu, the text does not scroll if the "SetUseVerticalScrollBar" is false.
How can I hide my scrollbar while maintaining it's functionality in a cross platform manner?
ScrollToLine seems to work consistently on Windows and Linux, so you could replace the ScrollLines call with something like this:
first = textViewer.GetFirstVisibleLine()
textViewer.ScrollToLine(first + n)
where n is the number of lines to scroll down.

Stock Icons not shown on buttons

self.button = gtk.Button(stock=gtk.STOCK_DELETE)
Only Shows:
Delete
The Python equivalent for setting the property without having to change any system config files is:
settings = gtk.settings_get_default()
settings.props.gtk_button_images = True
This should follow a call to window.show() and, obviously, precede the gtk.main() loop.
This is a recent change in GTK - the developers wanted icons not to appear on buttons. On Linux, this can be changed by editing the gconf key
/desktop/gnome/interface/buttons_have_icons
On windows, I think (I haven't actually tried this) that you need to set a value in your gtkrc file (for me it's in C:\Program Files\Gtk+\etc\gtkrc) and use a theme that supports icons (I think the default one doesn't).
You can also add gtk-button-images = 1 to your ~/.gtkrc-2.0 file after setting the theme which may over ride the option from gconf.
EDIT in answer to your comment:
Just like this answer, but in Python: In Gtk, how do I make a Button with just a stock icon?
For python, it's just
image = gtk.Image()
# (from http://www.pygtk.org/docs/pygtk/gtk-stock-items.html)
image.set_from_stock(gtk.STOCK_**)
button = gtk.Button()
button.set_image(image)
button.set_label("")
I had to do this to get it to work from Python without changing my config file. When I called set_image(), no image was being displayed.
image = gtk.Image()
image.set_from_stock(gtk.STOCK_**, gtk.ICON_SIZE_BUTTON)
button = gtk.Button()
button.add(image)
button.show()
If you work with pygobject, the new syntax is:
image.set_from_stock(gtk.STOCK_**, Gtk.IconSize.BUTTON)
I had the same issue in GTKmm on Windows. The "MS-Windows" theme disables images on stock buttons and the theme has priority over settings in gtkrc (so putting gtk-button-images = true in gtkrc didn't help). What I did is to modify the GTK settings runtime, and the images appeared as expected. :) Here's the code in C++:
Glib::RefPtr<Gtk::Settings> settings = Gtk::Settings::get_default();
/* force using icons on stock buttons: */
settings->property_gtk_button_images() = true;
It should be placed after the first window is constructed.
in Gtk3 gtk.STOCK method has been deprecated from v3.10.
Deprecated since version 3.10: Use Gtk.Button.new_with_label ()
instead.
In the case it doesn't help since it points to the custom label solution (new_with_label) If you want to use STOCK stuff you still can do so with new methods Gtk.Button.new_from_icon_name(icon_name, size) and Gtk.Button.new_with_mnemonic(label) which will create new buttons with stock icon and label respectively.
Example new button with a "stock" icon:
button = Gtk.Button.new_from_icon_name ("edit-paste", Gtk.IconSize.SMALL_TOOLBAR)
Example new button with a "stock" label:
button = Gtk.Button.new_with_mnemonic("_Open")
NOTE: on serious code creating a constant variable instead of using the string straight is a better option :)
References:
Gtk.Button
static new_with_mnemonic(label)
new_from_icon_name(icon_name, size)
Freedesktops Naming Convention
You can show explicitly the button image, justly, Gtk+ developers do not recommend doing this because it's overrides the Gtk+ user configuration.
So...
button.get_image().show()

How to embed some application window in my application using any Python GUI framework

I want some application to look like widget inside my Python application.
That's all. I dont need any interaction between them. I'm interested in solutions in any GUI toolkit for both windows and x windows.
It would be nice to have a solution with Tkinter but it's not crucial.
Using GTK on X windows (i.e. Linux, FreeBSD, Solaris), you can use the XEMBED protocol to embed widgets using gtk.Socket. Unfortunately, the application that you're launching has to explicitly support it so that you can tell it to embed itself. Some applications don't support this. Notably, I can't find a way to do it with Firefox.
Nonetheless, here's a sample program that will run either an X terminal or an Emacs session inside a GTK window:
import os
import gtk
from gtk import Socket, Button, Window, VBox, HBox
w = Window()
e = Button("Emacs")
x = Button("XTerm")
s = Socket()
v = VBox()
h = HBox()
w.add(v)
v.add(s)
h.add(e)
h.add(x)
v.pack_start(h, expand=False)
def runemacs(btn):
x.set_sensitive(False); e.set_sensitive(False)
os.spawnlp(os.P_NOWAIT, "emacs",
"emacs", "--parent-id", str(s.get_id()))
def runxterm(btn):
x.set_sensitive(False); e.set_sensitive(False)
os.spawnlp(os.P_NOWAIT, "xterm",
"xterm", "-into", str(s.get_id()))
e.connect('clicked', runemacs)
x.connect('clicked', runxterm)
w.show_all()
gtk.main()
Not enough reputation to comment on Glyphs answer. To make xterm work, in addition to the comments above one needs to also add
XTerm*allowSendEvents: True
to ~/.Xresources. (and perhaps reload those, with xrdb -load ~/.Xresources)

Categories

Resources