@@ -2,30 +2,39 @@ pub(crate) use self::_tkinter::make_module;
22
33#[ pymodule]
44mod _tkinter {
5- use crate :: vm:: VirtualMachine ;
65 use crate :: builtins:: PyTypeRef ;
7- use tk:: * ;
6+ use rustpython_vm:: { PyResult , VirtualMachine , function:: OptionalArg } ;
7+ use rustpython_vm:: function:: { Either , FuncArgs } ;
8+
9+ use crate :: common:: lock:: PyRwLock ;
10+ use std:: sync:: Arc ;
811 use tk:: cmd:: * ;
12+ use tk:: * ;
913
1014 #[ pyattr]
1115 const TK_VERSION : & str = "8.6" ;
1216 #[ pyattr]
1317 const TCL_VERSION : & str = "8.6" ;
18+ #[ pyattr]
19+ const READABLE : i32 = 2 ;
20+ #[ pyattr]
21+ const WRITABLE : i32 = 4 ;
22+ #[ pyattr]
23+ const EXCEPTION : i32 = 8 ;
1424
1525 fn demo ( ) -> tk:: TkResult < ( ) > {
1626 let tk = make_tk ! ( ) ?;
1727 let root = tk. root ( ) ;
18- root. add_label ( -text ( "constructs widgets and layout step by step" ) ) ?
19- . pack ( ( ) ) ?;
20- let f = root
21- . add_frame ( ( ) ) ?
28+ root. add_label ( -text ( "constructs widgets and layout step by step" ) ) ?
2229 . pack ( ( ) ) ?;
30+ let f = root. add_frame ( ( ) ) ?. pack ( ( ) ) ?;
2331 let _btn = f
24- . add_button ( "btn" -text ( "quit" ) -command ( "destroy ." ) ) ?
32+ . add_button ( "btn" - text ( "quit" ) - command ( "destroy ." ) ) ?
2533 . pack ( ( ) ) ?;
2634 Ok ( main_loop ( ) )
2735 }
2836
37+ // TODO: Remove once enough has been implemented.
2938 #[ pyfunction]
3039 fn tk_demo ( ) {
3140 let _ = demo ( ) ;
@@ -39,4 +48,48 @@ mod _tkinter {
3948 Some ( vec ! [ vm. ctx. exceptions. exception_type. to_owned( ) ] ) ,
4049 )
4150 }
51+
52+ #[ pyfunction]
53+ fn create (
54+ args : FuncArgs ,
55+ _vm : & VirtualMachine ,
56+ ) -> PyResult < TkApp > {
57+ // TODO: handle arguements
58+ // TODO: this means creating 2 tk instances is not possible.
59+ let tk = Tk :: new ( ( ) ) . unwrap ( ) ;
60+ Ok ( TkApp {
61+ tk : Arc :: new ( PyRwLock :: new ( tk) ) ,
62+ } )
63+ }
64+
65+ #[ pyattr]
66+ #[ pyclass( name = "tkapp" ) ]
67+ #[ derive( PyPayload ) ]
68+ struct TkApp {
69+ tk : Arc < PyRwLock < tk:: Tk < ( ) > > > ,
70+ }
71+
72+ unsafe impl Send for TkApp { }
73+
74+ unsafe impl Sync for TkApp { }
75+
76+ impl std:: fmt:: Debug for TkApp {
77+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
78+ f. debug_struct ( "TkApp" ) . finish ( )
79+ }
80+ }
81+
82+ #[ pyclass]
83+ impl TkApp {
84+ #[ pymethod]
85+ fn getvar ( & self , name : & str ) -> PyResult < String > {
86+ let tk = self . tk . read ( ) . unwrap ( ) ;
87+ Ok ( tk. getvar ( name) . unwrap ( ) )
88+ }
89+
90+ #[ pymethod]
91+ fn createcommand ( & self , name : String , callback : PyObjectRef ) {
92+
93+ }
94+ }
4295}
0 commit comments