8000 create and TkApp · RustPython/RustPython@16ae72c · GitHub
[go: up one dir, main page]

Skip to content

Commit 16ae72c

Browse files
committed
create and TkApp
1 parent bb48456 commit 16ae72c

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stdlib/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ bz2 = ["bzip2"]
1818
sqlite = ["dep:libsqlite3-sys"]
1919
ssl = ["openssl", "openssl-sys", "foreign-types-shared", "openssl-probe"]
2020
ssl-vendor = ["ssl", "openssl/vendored"]
21-
tkinter = ["dep:tk"]
21+
tkinter = ["dep:tk", "dep:tcl"]
2222

2323
[dependencies]
2424
# rustpython crates
@@ -86,6 +86,7 @@ bzip2 = { version = "0.4", optional = true }
8686

8787
# tkinter
8888
tk = { version = "0.1.10", optional = true }
89+
tcl = { version = "0.1.9", optional = true }
8990

9091
# uuid
9192
[target.'cfg(not(any(target_os = "ios", target_os = "android", target_os = "windows", target_arch = "wasm32", target_os = "redox")))'.dependencies]

stdlib/src/tkinter.rs

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,39 @@ pub(crate) use self::_tkinter::make_module;
22

33
#[pymodule]
44
mod _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

Comments
 (0)
0