forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtkinter.rs
More file actions
86 lines (73 loc) · 2.2 KB
/
tkinter.rs
File metadata and controls
86 lines (73 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// cspell:ignore createcommand
pub(crate) use self::_tkinter::make_module;
#[pymodule]
mod _tkinter {
use crate::builtins::PyTypeRef;
use rustpython_vm::function::{Either, FuncArgs};
use rustpython_vm::{PyResult, VirtualMachine, function::OptionalArg};
use crate::common::lock::PyRwLock;
use std::sync::Arc;
use tk::cmd::*;
use tk::*;
#[pyattr]
const TK_VERSION: &str = "8.6";
#[pyattr]
const TCL_VERSION: &str = "8.6";
#[pyattr]
const READABLE: i32 = 2;
#[pyattr]
const WRITABLE: i32 = 4;
#[pyattr]
const EXCEPTION: i32 = 8;
fn demo() -> tk::TkResult<()> {
let tk = make_tk!()?;
let root = tk.root();
root.add_label(-text("constructs widgets and layout step by step"))?
.pack(())?;
let f = root.add_frame(())?.pack(())?;
let _btn = f
.add_button("btn" - text("quit") - command("destroy ."))?
.pack(())?;
Ok(main_loop())
}
#[pyattr(once, name = "TclError")]
fn tcl_error(vm: &VirtualMachine) -> PyTypeRef {
vm.ctx.new_exception_type(
"zlib",
"TclError",
Some(vec![vm.ctx.exceptions.exception_type.to_owned()]),
)
}
#[pyfunction]
fn create(args: FuncArgs, _vm: &VirtualMachine) -> PyResult<TkApp> {
// TODO: handle arguments
// TODO: this means creating 2 tk instances is not possible.
let tk = Tk::new(()).unwrap();
Ok(TkApp {
tk: Arc::new(PyRwLock::new(tk)),
})
}
#[pyattr]
#[pyclass(name = "tkapp")]
#[derive(PyPayload)]
struct TkApp {
tk: Arc<PyRwLock<tk::Tk<()>>>,
}
unsafe impl Send for TkApp {}
unsafe impl Sync for TkApp {}
impl std::fmt::Debug for TkApp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TkApp").finish()
}
}
#[pyclass]
impl TkApp {
#[pymethod]
fn getvar(&self, name: &str) -> PyResult<String> {
let tk = self.tk.read().unwrap();
Ok(tk.getvar(name).unwrap())
}
#[pymethod]
fn createcommand(&self, name: String, callback: PyObjectRef) {}
}
}