📄 tk.rb
字号:
def value=(val) begin s = '"' + _get_eval_string(val).gsub(/[][$"]/, '\\\\\&') + '"' INTERP._eval(format('global %s; set %s %s', @id, @id, s)) rescue if INTERP._eval(format('global %s; array exists %s', @id, @id)) != "1" fail else if val == [] INTERP._eval(format('global %s; unset %s; set %s(0) 0; unset %s(0)', @id, @id, @id, @id)) elsif val.kind_of?(Array) a = [] val.each_with_index{|e,i| a.push(i); a.push(array2tk_list(e))} s = '"' + a.join(" ").gsub(/[][$"]/, '\\\\\&') + '"' INTERP._eval(format('global %s; unset %s; array set %s %s', @id, @id, @id, s)) elsif val.kind_of?(Hash) s = '"' + val.to_a.collect{|e| array2tk_list(e)}.join(" ")\ .gsub(/[][$"]/, '\\\\\&') + '"' INTERP._eval(format('global %s; unset %s; array set %s %s', @id, @id, @id, s)) else fail end end end end def [](index) INTERP._eval(format('global %s; set %s(%s)', @id, @id, _get_eval_string(index))) end def []=(index,val) INTERP._eval(format('global %s; set %s(%s) %s', @id, @id, _get_eval_string(index), _get_eval_string(val))) end def to_i number(value).to_i end def to_f number(value).to_f end def to_s string(value).to_s end def inspect format "#<TkVariable: %s>", @id end def ==(other) case other when TkVariable self.equal(self) when String self.to_s == other when Integer self.to_i == other when Float self.to_f == other when Array self.to_a == other else false end end def to_a list(value) end def to_eval @id end def unset(elem=nil) if elem INTERP._eval(format('global %s; unset %s(%s)', @id, @id, tk_tcl2ruby(elem))) else INTERP._eval(format('global %s; unset %s', @id, @id)) end end alias remove unset def trace_callback(elem, op) if @trace_var.kind_of? Array @trace_var.each{|m,e| e.call(self,elem,op) if m.index(op)} end if elem.kind_of? String if @trace_elem[elem].kind_of? Array @trace_elem[elem].each{|m,e| e.call(self,elem,op) if m.index(op)} end end end def trace(opts, cmd) @trace_var = [] if @trace_var == nil opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('') @trace_var.unshift([opts,cmd]) if @trace_opts == nil TkVar_CB_TBL[@id] = self @trace_opts = opts Tk.tk_call('trace', 'variable', @id, @trace_opts, 'rb_var') else newopts = @trace_opts.dup opts.each_byte{|c| newopts += c.chr unless newopts.index(c)} if newopts != @trace_opts Tk.tk_call('trace', 'vdelete', @id, @trace_opts, 'rb_var') @trace_opts.replace(newopts) Tk.tk_call('trace', 'variable', @id, @trace_opts, 'rb_var') end end end def trace_element(elem, opts, cmd) @trace_elem = {} if @trace_elem == nil @trace_elem[elem] = [] if @trace_elem[elem] == nil opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('') @trace_elem[elem].unshift([opts,cmd]) if @trace_opts == nil TkVar_CB_TBL[@id] = self @trace_opts = opts Tk.tk_call('trace', 'variable', @id, @trace_opts, 'rb_var') else newopts = @trace_opts.dup opts.each_byte{|c| newopts += c.chr unless newopts.index(c)} if newopts != @trace_opts Tk.tk_call('trace', 'vdelete', @id, @trace_opts, 'rb_var') @trace_opts.replace(newopts) Tk.tk_call('trace', 'variable', @id, @trace_opts, 'rb_var') end end end def trace_vinfo return [] unless @trace_var @trace_var.dup end def trace_vinfo_for_element(elem) return [] unless @trace_elem return [] unless @trace_elem[elem] @trace_elem[elem].dup end def trace_vdelete(opts,cmd) return unless @trace_var.kind_of? Array opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('') idx = -1 newopts = '' @trace_var.each_with_index{|e,i| if idx < 0 && e[0] == opts && e[1] == cmd idx = i next end e[0].each_byte{|c| newopts += c.chr unless newopts.index(c)} } if idx >= 0 @trace_var.delete_at(idx) else return end @trace_elem.each{|elem| @trace_elem[elem].each{|e| e[0].each_byte{|c| newopts += c.chr unless newopts.index(c)} } } newopts = ['r','w','u'].find_all{|c| newopts.index(c)}.join('') if newopts != @trace_opts Tk.tk_call('trace', 'vdelete', @id, @trace_opts, 'rb_var') @trace_opts.replace(newopts) if @trace_opts != '' Tk.tk_call('trace', 'variable', @id, @trace_opts, 'rb_var') end end end def trace_vdelete_for_element(elem,opts,cmd) return unless @trace_elem.kind_of? Hash return unless @trace_elem[elem].kind_of? Array opts = ['r','w','u'].find_all{|c| opts.index(c)}.join('') idx = -1 @trace_elem[elem].each_with_index{|e,i| if idx < 0 && e[0] == opts && e[1] == cmd idx = i next end } if idx >= 0 @trace_elem[elem].delete_at(idx) else return end newopts = '' @trace_var.each{|e| e[0].each_byte{|c| newopts += c.chr unless newopts.index(c)} } @trace_elem.each{|elem| @trace_elem[elem].each{|e| e[0].each_byte{|c| newopts += c.chr unless newopts.index(c)} } } newopts = ['r','w','u'].find_all{|c| newopts.index(c)}.join('') if newopts != @trace_opts Tk.tk_call('trace', 'vdelete', @id, @trace_opts, 'rb_var') @trace_opts.replace(newopts) if @trace_opts != '' Tk.tk_call('trace', 'variable', @id, @trace_opts, 'rb_var') end end endendclass TkVarAccess<TkVariable def initialize(varname, val=nil) @id = varname if val s = '"' + _get_eval_string(val).gsub(/[][$"]/, '\\\\\&') + '"' #" INTERP._eval(format('global %s; set %s %s', @id, @id, s)) end endendmodule Tk begin auto_path = INTERP._invoke('set', 'auto_path') rescue begin auto_path = INTERP._invoke('set', 'env(TCLLIBPATH)') rescue auto_path = Tk::LIBRARY end end AUTO_PATH = TkVarAccess.new('auto_path', auto_path) TCL_PACKAGE_PATH = TkVarAccess.new('tcl_pkgPath')endmodule TkSelection include Tk extend Tk def clear(win=Tk.root) tk_call 'selection', 'clear', win.path end def get(type=None) tk_call 'selection', 'get', type end def TkSelection.handle(win, func, type=None, format=None) tk_call 'selection', 'handle', win.path, func, type, format end def handle(func, type=None, format=None) TkSelection.handle self, func, type, format end def TkSelection.own(win=None, func=None) window(tk_call 'selection', 'own', win, func) end def own(func=None) TkSelection.own self, func end module_function :clear, :getendmodule TkKinput include Tk extend Tk def TkKinput.start(window, style=None) tk_call 'kinput_start', window.path, style end def kinput_start(style=None) TkKinput.start(self, style) end def TkKinput.send_spot(window) tk_call 'kinput_send_spot', window.path end def kinput_send_spot TkKinput.send_spot(self) end def TkKinput.input_start(window, keys=nil) tk_call 'kanjiInput', 'start', window.path, *hash_kv(keys) end def kanji_input_start(keys=nil) TkKinput.input_start(self, keys) end def TkKinput.attribute_config(window, slot, value=None) if slot.kind_of? Hash tk_call 'kanjiInput', 'attribute', window.path, *hash_kv(slot) else tk_call 'kanjiInput', 'attribute', window.path, "-#{slot}", value end end def kinput_attribute_config(slot, value=None) TkKinput.attribute_config(self, slot, value) end def TkKinput.attribute_info(window, slot=nil) if slot conf = tk_split_list(tk_call('kanjiInput', 'attribute', window.path, "-#{slot}")) conf[0] = conf[0][1..-1] conf else tk_split_list(tk_call('kanjiInput', 'attribute', window.path)).collect{|conf| conf[0] = conf[0][1..-1] conf } end end def kinput_attribute_info(slot=nil) TkKinput.attribute_info(self, slot) end def TkKinput.input_end(window) tk_call 'kanjiInput', 'end', window.path end def kanji_input_end TkKinput.input_end(self) endendmodule TkXIM include Tk extend Tk def TkXIM.useinputmethods(window=nil, value=nil) if window if value tk_call 'tk', 'useinputmethods', '-displayof', window.path, value else tk_call 'tk', 'useinputmethods', '-displayof', window.path end else if value tk_call 'tk', 'useinputmethods', value else tk_call 'tk', 'useinputmethods' end end end def TkXIM.configure(window, slot, value=None) begin if /^8\.*/ === Tk::TK_VERSION && JAPANIZED_TK if slot.kind_of? Hash tk_call 'imconfigure', window.path, *hash_kv(slot) else tk_call 'imconfigure', window.path, "-#{slot}", value end end rescue end end def TkXIM.configinfo(window, slot=nil) begin if /^8\.*/ === Tk::TK_VERSION && JAPANIZED_TK if slot conf = tk_split_list(tk_call('imconfigure', window.path, "-#{slot}")) conf[0] = conf[0][1..-1] conf else tk_split_list(tk_call('imconfigure', window.path)).collect{|conf| conf[0] = conf[0][1..-1] conf } end else [] end rescue [] end end def useinputmethods(value=nil) TkXIM.useinputmethods(self, value) end def imconfigure(window, slot, value=None) TkXIM.configinfo(window, slot, value) end def imconfiginfo(slot=nil) TkXIM.configinfo(window, slot) endendmodule TkWinfo include Tk extend Tk def TkWinfo.atom(name) number(tk_call 'winfo', 'atom', name) end def winfo_atom(name) TkWinfo.atom name end def TkWinfo.atomname(id) tk_call 'winfo', 'atomname', id end def winfo_atomname(id) TkWinfo.atomname id end def TkWinfo.cells(window) number(tk_call('winfo', 'cells', window.path)) end def winfo_cells TkWinfo.cells self end def TkWinfo.children(window) c = tk_call('winfo', 'children', window.path) list(c) end def winfo_children TkWinfo.children self end def TkWinfo.classname(window) tk_call 'winfo', 'class', window.path end def winfo_classname TkWinfo.classname self end def TkWinfo.colormapfull(window) bool(tk_call('winfo', 'colormapfull', window.path)) end def winfo_colormapfull TkWinfo.colormapfull self end def TkWinfo.containing(rootX, rootY) path = tk_call('winfo', 'containing', rootX, rootY) window(path) end def winfo_containing(x, y) TkWinfo.containing x, y end def TkWinfo.depth(window) number(tk_call('winfo', 'depth', window.path)) end def winfo_depth TkWinfo.depth self end def TkWinfo.exist?(window) bool(tk_call('winfo', 'exists', window.path)) end def winfo_exist? TkWinfo.exist? self end def TkWinfo.fpixels(window, number) number(tk_call('winfo', 'fpixels', window.path, number)) end def winfo_fpixels(number) TkWinfo.fpixels self, number end def TkWinfo.geometry(window) tk_call('winfo', 'geometry', window.path) end def winfo_geometry TkWinfo.geometry self end def TkWinfo.height(window) number(tk_call('winfo', 'height', window.path)) end def winfo_height TkWinfo.height self end def TkWinfo.id(window) tk_call('winfo', 'id', window.path) end def winfo_id TkWinfo.id self end def TkWinfo.interps(window=nil) if window tk_split_simplelist(tk_call('winfo', 'interps', '-displayof', window.path)) else tk_split_simplelist(tk_call('winfo', 'interps')) end end def winfo_interps TkWinfo.interps self end def TkWinfo.mapped?(window) bool(tk_call('winfo', 'ismapped', window.path)) end def winfo_mapped? TkWinfo.mapped? self end def TkWinfo.manager(window) tk_call('winfo', 'manager', window.path) end def winfo_manager TkWinfo.manager self end def TkWinfo.appname(window) tk_call('winfo', 'name', window.path) end def winfo_appname TkWinfo.appname self end def TkWinfo.parent(window) window(tk_call('winfo', 'parent', window.path)) end def winfo_parent TkWinfo.parent self end def TkWinfo.widget(id) window(tk_call('winfo', 'pathname', id)) end def winfo_widget(id) TkWinfo.widget id end def TkWinfo.pixels(window, number) number(tk_call('winfo', 'pixels', window.path, number)) end def winfo_pixels(number) TkWinfo.pixels self, number end def TkWinfo.reqheight(window) number(tk_call('winfo', 'reqheight', window.path)) end def winfo_reqheight TkWinfo.reqheight self end def TkWinfo.reqwidth(window) number(tk_call('winfo', 'reqwidth', window.path)) end def winfo_reqwidth TkWinfo.reqwidth self end def TkWinfo.rgb(window, color) list(tk_call('winfo', 'rgb', window.path, color)) end def winfo_rgb(color) TkWinfo.rgb self, color end def TkWinfo.rootx(window) number(tk_call('winfo', 'rootx', window.path)) end def winfo_rootx TkWinfo.rootx self end def TkWinfo.rooty(window) number(tk_call('winfo', 'rooty', window.path)) end def winfo_rooty TkWinfo.rooty self end def TkWinfo.screen(window) tk_call 'winfo', 'screen', window.path end def winfo_screen TkWinfo.screen self end def TkWinfo.screencells(window) number(tk_call('winfo', 'screencells', window.path)) end def winfo_screencells TkWinfo.screencells self end def TkWinfo.screendepth(window) number(tk_call('winfo', 'screendepth', window.path)) end def winfo_screendepth TkWinfo.screendepth self end def TkWinfo.screenheight (window) number(tk_call('winfo', 'screenheight', window.path)) end def winfo_screenheight TkWinfo.screenheight self
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -