2017-11-17 142 views
1

因此,我正在编写一个小的Racket应用程序,该应用程序将解析(格式非常差的).txt文件并输出可在Excel中使用的.csv。我想要做的第一件事是打开一个带有一个按钮的小窗口,该按钮打开一个文件对话框,以便用户可以选择要转换的文件(就像任何启动打开的文件选择对话框的程序一样)。我在网上查了一下,找不到任何东西。这将是一个本地应用程序,所以我在POST服务器上找到的东西并不相关。你怎么能在球拍上做到这一点?如何在球拍中创建文件上传按钮?

回答

0

程序get-fileput-file存在于#lang racket/gui中。两者都可以用来通过对话框从用户获取文件路径名。 get-file是针对选择现有文件而设计的,而put-file则针对创建新文件。

考虑下面的例子,执行以下操作:

  • 它创建包含“选择文件”按钮的窗口。
  • 单击按钮时,会出现一个对话框来选择文件(可以是用于转换的.txt文件)。
  • 一旦选择了文本文件,它将打开另一个对话框来选择一个文件来保存转换后的数据(可以是.csv文件)。

#lang racket/gui 

;; Prints file's contents line by line 
(define (print-each-line input-file) 
    (define line (read-line input-file)) 
    (unless (eof-object? line) 
    ;; 'line' here represents a line of txt file's contents 
    ;; The line can be processed/modified to any desired output, but 
    ;; for the purposes of this example, the line will simply be 
    ;; printed the way it is without any "processing". 
    (println line) 
    (print-each-line input-file))) 

;; Convert txt to csv by printing each line of txt 
;; to the csv file using print-each-line (above) 
(define (convert txt csv) 
    (define in (open-input-file txt)) 
    (with-output-to-file csv 
    (lambda() (print-each-line in))) 
    (close-input-port in)) 

;; Make a frame by instantiating the frame% class 
(define frame (new frame% [label "Example"])) 

;; Make a button in the frame 
(new button% [parent frame] 
      [label "Select File"] 
      ;; Callback procedure for a button click: 
      [callback 
       (lambda (button event) 
       (define txt (get-file)) 
       (define csv (put-file)) 
       (convert txt csv))]) 

;; Show the frame by calling its show method 
(send frame show #t) 
+0

太感谢你了!这正是我需要了解它如何工作的。我现在阅读更多关于框架的内容。感谢您的帮助 – matzy

+0

不客气:) – assefamaru

0

目前尚不清楚您需要什么。如果您想知道如何使用文件,请参见File ports部分,但如果您需要知道如何创建和使用GUI对象,请参见Windowing。基于GTK构建GUI应用程序(在球拍实现中使用)对于新用户来说这不是一件容易的事情。这不是我的生意,但我认为如果你拿一些RAD(比如Lazarus用于对象pascal,MS Visual Studio for C#),那么比没有经验的编写GUI文本更快更容易。