if you saw my earlier email.....

3 views
Skip to first unread message

Mitch Raful

unread,
Feb 18, 2021, 10:23:47 PMFeb 18
to begin...@googlegroups.com
The code I sent earlier is wrong.  I didn't need a new function...

extern crate gtk;
use gtk::prelude::*;
use gtk::{Button, Window, WindowType};
use std::io::{Write, ErrorKind};
use std::fs::OpenOptions;

fn main() {
    if gtk::init().is_err() {
        println!("Failed to initialize GTK.");
        return;
    }

    let window = Window::new(WindowType::Toplevel);
    window.set_title("First GTK+ Program");
    window.set_default_size(350, 70);
    let button = Button::new_with_label("Click me");
    window.add(&button);
    window.show_all();

    window.connect_delete_event(|_, _| {
        gtk::main_quit();
        Inhibit(false)
    });

    button.connect_clicked(|_| {
        //let mut file = std::fs::File::create("mylog.txt")
        //    .expect("Couldn't create file!");
        let mut file = OpenOptions::new()
            .append(true)
            .open("mylog.txt")
            .unwrap_or_else(|error| {
                if error.kind() == ErrorKind::NotFound {
                    std::fs::File::create("mylog.txt")
                        .unwrap_or_else(|error| {
                            panic!("Problem creating file: {:?}", error);
                        })
                } else {
                    panic!("Can't open file: {:?}", error);
                }
            });
        match file.write(b"I was clicked.\n") {
            Err(e) => panic!("Can't write to file: {}", e),
            Ok(_) => {println!("Wrote to file")}
        };
    });

    gtk::main();

}
/*
fn write_me(mut f: File, line: &[u8; 15]) -> std::io::Result<()>{
    f.write_all(line)?;
    Ok(())
}
*/


Mitch
Reply all
Reply to author
Forward
0 new messages