Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

_tkinter pt. 1 #5583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
create and TkApp
  • Loading branch information
arihant2math authored and youknowone committed Mar 23, 2025
commit 52b85e6d5b9f5f5c04c375178f333a0b2334806a
1 change: 1 addition & 0 deletions 1 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion 3 stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bz2 = ["bzip2"]
sqlite = ["dep:libsqlite3-sys"]
ssl = ["openssl", "openssl-sys", "foreign-types-shared", "openssl-probe"]
ssl-vendor = ["ssl", "openssl/vendored"]
tkinter = ["dep:tk"]
tkinter = ["dep:tk", "dep:tcl"]

[dependencies]
# rustpython crates
Expand Down Expand Up @@ -86,6 +86,7 @@ bzip2 = { version = "0.4", optional = true }

# tkinter
tk = { version = "0.1.10", optional = true }
tcl = { version = "0.1.9", optional = true }

# uuid
[target.'cfg(not(any(target_os = "ios", target_os = "android", target_os = "windows", target_arch = "wasm32", target_os = "redox")))'.dependencies]
Expand Down
67 changes: 60 additions & 7 deletions 67 stdlib/src/tkinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,39 @@ pub(crate) use self::_tkinter::make_module;

#[pymodule]
mod _tkinter {
use crate::vm::VirtualMachine;
use crate::builtins::PyTypeRef;
use tk::*;
use rustpython_vm::{PyResult, VirtualMachine, function::OptionalArg};
use rustpython_vm::function::{Either, FuncArgs};

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(())?
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 .") )?
.add_button("btn" - text("quit") - command("destroy ."))?
.pack(())?;
Ok(main_loop())
}

// TODO: Remove once enough has been implemented.
#[pyfunction]
fn tk_demo() {
let _ = demo();
Expand All @@ -39,4 +48,48 @@ mod _tkinter {
Some(vec![vm.ctx.exceptions.exception_type.to_owned()]),
)
}

#[pyfunction]
fn create(
args: FuncArgs,
_vm: &VirtualMachine,
) -> PyResult<TkApp> {
// TODO: handle arguements
// 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) {

}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.