我正在使用Ubuntu 10.04并且我正在尝试运行一个WxRuby示例,我从此站点WxRubyWiki复制粘贴。我一直在寻找网上的帮助,但我找不到类似的东西......
我收到了这个错误...
Gtk:ERROR:/build/buildd/gtk+2.0-2.20.1/gtk/gtkwindow.c:6789:IA__gtk_window_present_with_time: assertion failed: (widget->window != NULL)
Aborted
这些是我正在使用的版本......
ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
gem list
...
wxruby (2.0.1 x86-linux)
...
这是我正在尝试运行的代码......
require 'rubygems' if RUBY_VERSION < '1.9'
require 'wx'
class EventFrame < Wx::Frame
def initialize()
super(nil, -1, "Event Frame")
@idleCounter = 0
evt_close {|event| on_close(event)}
evt_idle {|event| on_idle(event)}
evt_size {|event| on_size(event)}
evt_key_down {|event| on_key(event)}
evt_left_down {|event| on_left_down(event)}
# You can still process these events, you just need to define a separate callback for middle_down and right_down
# to process them as separate events
evt_middle_down {|event| on_middle_down(event)}
evt_right_down {|event| on_right_down(event)}
button = Wx::Button.new(self, -1, "Push me")
evt_button(button.get_id()) {|event| on_button(event)}
show()
end
def message(text, title)
m = Wx::MessageDialog.new(self, text, title, Wx::OK | Wx::ICON_INFORMATION)
m.show_modal()
end
def on_close(event)
message("This frame will be closed after you push ok", "Close event")
#close(true) - Don't call this - it will call on_close again, and your application will be caught in an infinite loop
# Either call event.skip() to allow the Frame to close, or call destroy(), as follows
destroy()
end
def on_idle(event)
@idleCounter += 1
if @idleCounter > 15 # Without the counter to slow this down, Idle events would be firing every second
message("The system is idle right now", "Idle event")
@idleCounter = 0
end
event.request_more() # You must include this, otherwise the Idle event won't occur again
end
def on_size(event)
size = event.get_size()
x = size.x
y = size.y
message("X = " + x.to_s + ", Y = " + y.to_s, "Size event")
end
def on_key(event)
message("Key pressed", "Key Event")
end
def on_left_down(event)
button = ""
if event.left_down()
button = "Left"
end
message(button + " button was clicked", "Mouse event")
end
def on_middle_down(event)
# This method hasn't been implemented yet...
#if event.middle_down()
#button = "Middle"
#end
message("Middle button was clicked", "Mouse event")
end
def on_right_down(event)
# This method hasn't been implemented yet...
#if event.right_down()
#button = "Right"
#end
message("Right button was clicked", "Mouse event")
end
def on_button(event)
message("Button was clicked", "Button event")
end
end
class MyApp < Wx::App
def on_init
EventFrame.new()
end
end
MyApp.new.main_loop
提前致谢!
答案 0 :(得分:1)
更多的是了解GTK + 2 / wx / wxRuby的工作原理。因此,上面的代码不适用于我设置为测试的虚拟盒机器中的配置,也不适用于我的开发机器的具有1000Hz内核编译选项和ruby 1.9.3 p21的Ubuntu 11.10 x86_64。
在帧创建期间触发on_size事件时发生GTK + 2错误。创建尚未完成,因此消息框当时没有父级。你可以通过注释来测试它(在def on_size(event)中:
message("X = " + x.to_s + ", Y = " + y.to_s, "Size event")
并尝试:
puts "Size event: X = #{x}, Y = #{y}"
查看标准输出的活动详情。您会注意到在创建过程中有两个事件被触发;初始大小事件和调整大小事件
另一个警告是空闲循环,它将Unity锁定在我的系统中。您可以通过更改以下代码来测试空闲事件是否在没有锁定的情况下发生:
在show()之前添加:
create_status_bar(2)
self.status_text = "Welcome to wxRuby!"
然后在idle_event中:
def on_idle(event)
@idleCounter += 1
#if @idleCounter > 15 # Without the counter to slow this down, Idle events would be firing every second
# message("The system is idle right now", "Idle event")
# @idleCounter = 0
#end
set_status_text @idleCounter.to_s, 1
event.request_more() # You must include this, otherwise the Idle event won't occur again
end
关于代码的最后一个警告是,您可能会注意到您没有看到创建其消息框的键或鼠标按下事件。这是因为按钮控件填充客户区并捕获按键和按钮的帧事件。如果您在应用程序运行时调整框架大小,则默认情况下按钮不会调整大小(GTK + 2平台)。如果然后在框架客户区域内单击但未在按钮上单击,则会看到鼠标事件。
祝你好运!
答案 1 :(得分:0)
这个修复并在ubuntu 11.10中运行:)