@@ -10,10 +10,10 @@ one when using this package.
10
10
If `CONFIG.qtfix` is true, then this is run automatically before `PyQt4`, `PyQt5`, `PySide` or `PySide2` are imported.
11
11
"""
12
12
function fix_qt_plugin_path ()
13
- CONFIG . exepath === nothing && return false
14
- e = pyosmodule () . environ
13
+ C . CTX . exe_path === nothing && return false
14
+ e = pyosmodule. environ
15
15
" QT_PLUGIN_PATH" in e && return false
16
- qtconf = joinpath (dirname (CONFIG . exepath ), " qt.conf" )
16
+ qtconf = joinpath (dirname (C . CTX . exe_path ), " qt.conf" )
17
17
isfile (qtconf) || return false
18
18
for line in eachline (qtconf)
19
19
m = match (r" ^\s *prefix\s *=(.*)$" i , line)
@@ -30,58 +30,39 @@ function fix_qt_plugin_path()
30
30
return false
31
31
end
32
32
33
- """
34
- pyinteract(; force=false, sleep=0.1)
33
+ # """
34
+ # pyinteract(; force=false, sleep=0.1)
35
35
36
- Some Python GUIs can work interactively, meaning the GUI is available but the interactive prompt is returned (e.g. after calling `matplotlib.pyplot.ion()`).
37
- To use these from Julia, currently you must manually call `pyinteract()` each time you want to interact.
36
+ # Some Python GUIs can work interactively, meaning the GUI is available but the interactive prompt is returned (e.g. after calling `matplotlib.pyplot.ion()`).
37
+ # To use these from Julia, currently you must manually call `pyinteract()` each time you want to interact.
38
38
39
- Internally, this is calling the `PyOS_InputHook` asynchronously. Only one copy is run at a time unless `force` is true.
39
+ # Internally, this is calling the `PyOS_InputHook` asynchronously. Only one copy is run at a time unless `force` is true.
40
40
41
- The asynchronous task waits for `sleep` seconds before calling the hook function.
42
- This gives time for the next prompt to be printed and waiting for input.
43
- As a result, there will be a small delay before the GUI becomes interactive.
44
- """
45
- pyinteract (; force:: Bool = false , sleep:: Real = 0.1 ) =
46
- if ! CONFIG. inputhookrunning || force
47
- CONFIG. inputhookrunning = true
48
- @async begin
49
- sleep > 0 && Base. sleep (sleep)
50
- C. PyOS_RunInputHook ()
51
- CONFIG. inputhookrunning = false
52
- end
53
- nothing
54
- end
55
- export pyinteract
41
+ # The asynchronous task waits for `sleep` seconds before calling the hook function.
42
+ # This gives time for the next prompt to be printed and waiting for input.
43
+ # As a result, there will be a small delay before the GUI becomes interactive.
44
+ # """
45
+ # pyinteract(; force::Bool = false, sleep::Real = 0.1) =
46
+ # if !CONFIG.inputhookrunning || force
47
+ # CONFIG.inputhookrunning = true
48
+ # @async begin
49
+ # sleep > 0 && Base.sleep(sleep)
50
+ # C.PyOS_RunInputHook()
51
+ # CONFIG.inputhookrunning = false
52
+ # end
53
+ # nothing
54
+ # end
55
+ # export pyinteract
56
56
57
57
const EVENT_LOOPS = Dict {Symbol,Base.Timer} ()
58
58
59
- """
60
- event_loop_off(g::Symbol)
59
+ const new_event_loop_callback = pynew ()
61
60
62
- Terminate the event loop `g` if it is running.
63
- """
64
- function event_loop_off (g:: Symbol )
65
- if haskey (EVENT_LOOPS, g)
66
- Base. close (pop! (EVENT_LOOPS, g))
67
- end
68
- return
69
- end
70
-
71
- """
72
- event_loop_on(g::Symbol; interval=40e-3, fix=false)
73
-
74
- Activate an event loop for the GUI framework `g`, so that the framework can run in the background of a Julia session.
75
-
76
- The event loop runs every `interval` seconds. If `fix` is true and `g` is a Qt framework, then [`fix_qt_plugin_path`](@ref) is called.
77
-
78
- Supported values of `g` (and the Python module they relate to) are: `:pyqt4` (PyQt4), `:pyqt5` (PyQt5), `:pyside` (PySide), `:pyside2` (PySide2), `:gtk` (gtk), `:gtk3` (gi), `:wx` (wx), `:tkinter` (tkinter).
79
- """
80
- function event_loop_on (g:: Symbol ; interval:: Real = 40e-3 , fix:: Bool = false )
81
- haskey (EVENT_LOOPS, g) && return EVENT_LOOPS[g]
82
- fix && g in (:pyqt4 , :pyqt5 , :pyside , :pyside2 ) && fix_qt_plugin_path ()
83
- @py ```
84
- def make_event_loop(g, interval):
61
+ function init_gui ()
62
+ # define callbacks
63
+ @py g = {}
64
+ @py @exec """
65
+ def new_event_loop_callback(g, interval=0.04):
85
66
if g in ("pyqt4","pyqt5","pyside","pyside2"):
86
67
if g == "pyqt4":
87
68
import PyQt4.QtCore as QtCore
@@ -95,7 +76,7 @@ function event_loop_on(g::Symbol; interval::Real = 40e-3, fix::Bool = false)
95
76
AllEvents = QtCore.QEventLoop.AllEvents
96
77
processEvents = QtCore.QCoreApplication.processEvents
97
78
maxtime = interval * 1000
98
- def event_loop ():
79
+ def callback ():
99
80
app = instance()
100
81
if app is not None:
101
82
app._in_event_loop = True
@@ -110,15 +91,15 @@ function event_loop_on(g::Symbol; interval::Real = 40e-3, fix::Bool = false)
110
91
import gtk
111
92
events_pending = gtk.events_pending
112
93
main_iteration = gtk.main_iteration
113
- def event_loop ():
94
+ def callback ():
114
95
while events_pending():
115
96
main_iteration()
116
97
elif g == "wx":
117
98
import wx
118
99
GetApp = wx.GetApp
119
100
EventLoop = wx.EventLoop
120
101
EventLoopActivator = wx.EventLoopActivator
121
- def event_loop ():
102
+ def callback ():
122
103
app = GetApp()
123
104
if app is not None:
124
105
app._in_event_loop = True
@@ -133,7 +114,7 @@ function event_loop_on(g::Symbol; interval::Real = 40e-3, fix::Bool = false)
133
114
import tkinter, _tkinter
134
115
flag = _tkinter.ALL_EVENTS | _tkinter.DONT_WAIT
135
116
root = None
136
- def event_loop ():
117
+ def callback ():
137
118
global root
138
119
new_root = tkinter._default_root
139
120
if new_root is not None:
@@ -143,8 +124,42 @@ function event_loop_on(g::Symbol; interval::Real = 40e-3, fix::Bool = false)
143
124
pass
144
125
else:
145
126
raise ValueError("invalid event loop name: {}".format(g))
146
- return event_loop
147
- $event_loop = make_event_loop($(string (g)) , $interval )
148
- ```
149
- EVENT_LOOPS[g] = Timer (t -> event_loop (), 0 ; interval = interval)
127
+ return callback
128
+ """ g
129
+ pycopy! (new_event_loop_callback, g[" new_event_loop_callback" ])
130
+
131
+ # add a hook to automatically call fix_qt_plugin_path()
132
+ fixqthook = Py (() -> (fix_qt_plugin_path (); nothing ))
133
+ pymodulehooks. add_hook (" PyQt4" , fixqthook)
134
+ pymodulehooks. add_hook (" PyQt5" , fixqthook)
135
+ pymodulehooks. add_hook (" PySide" , fixqthook)
136
+ pymodulehooks. add_hook (" PySide2" , fixqthook)
137
+ end
138
+
139
+ """
140
+ event_loop_off(g::Symbol)
141
+
142
+ Terminate the event loop `g` if it is running.
143
+ """
144
+ function event_loop_off (g:: Symbol )
145
+ if haskey (EVENT_LOOPS, g)
146
+ Base. close (pop! (EVENT_LOOPS, g))
147
+ end
148
+ return
149
+ end
150
+
151
+ """
152
+ event_loop_on(g::Symbol; interval=0.04, fix=false)
153
+
154
+ Activate an event loop for the GUI framework `g`, so that the framework can run in the background of a Julia session.
155
+
156
+ The event loop runs every `interval` seconds. If `fix` is true and `g` is a Qt framework, then [`fix_qt_plugin_path`](@ref) is called.
157
+
158
+ Supported values of `g` (and the Python module they relate to) are: `:pyqt4` (PyQt4), `:pyqt5` (PyQt5), `:pyside` (PySide), `:pyside2` (PySide2), `:gtk` (gtk), `:gtk3` (gi), `:wx` (wx), `:tkinter` (tkinter).
159
+ """
160
+ function event_loop_on (g:: Symbol ; interval:: Real = 0.04 , fix:: Bool = false )
161
+ haskey (EVENT_LOOPS, g) && return EVENT_LOOPS[g]
162
+ fix && g in (:pyqt4 , :pyqt5 , :pyside , :pyside2 ) && fix_qt_plugin_path ()
163
+ callback = new_event_loop_callback (string (g), Float64 (interval))
164
+ EVENT_LOOPS[g] = Timer (t -> callback (), 0 ; interval = interval)
150
165
end
0 commit comments