@@ -2,30 +2,39 @@ pub(crate) use self::_tkinter::make_module;
2
2
3
3
#[ pymodule]
4
4
mod _tkinter {
5
- use crate :: vm:: VirtualMachine ;
6
5
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 ;
8
11
use tk:: cmd:: * ;
12
+ use tk:: * ;
9
13
10
14
#[ pyattr]
11
15
const TK_VERSION : & str = "8.6" ;
12
16
#[ pyattr]
13
17
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 ;
14
24
15
25
fn demo ( ) -> tk:: TkResult < ( ) > {
16
26
let tk = make_tk ! ( ) ?;
17
27
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" ) ) ?
22
29
. pack ( ( ) ) ?;
30
+ let f = root. add_frame ( ( ) ) ?. pack ( ( ) ) ?;
23
31
let _btn = f
24
- . add_button ( "btn" -text ( "quit" ) -command
8000
span>( "destroy ." ) ) ?
32
+ . add_button ( "btn" - text ( "quit" ) - command ( "destroy ." ) ) ?
25
33
. pack ( ( ) ) ?;
26
34
Ok ( main_loop ( ) )
27
35
}
28
36
37
+ // TODO: Remove once enough has been implemented.
29
38
#[ pyfunction]
30
39
fn tk_demo ( ) {
31
40
let _ = demo ( ) ;
@@ -39,4 +48,48 @@ mod _tkinter {
39
48
Some ( vec ! [ vm. ctx. exceptions. exception_type. to_owned( ) ] ) ,
40
49
)
41
50
}
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
+ }
42
95
}
0 commit comments