我正在尝试从 a 选择多个项目Listbox,在选择时按 Shift 键并选择一组项目似乎很直观,但 Tkinter 中似乎没有内置此功能。
所以我试图通过注册 Shift 键并获取最新的选择来自己实现它。但我在试图找出Listbox. listbox.get(ACTIVE)似乎落后于我的预期。
这是我到目前为止尝试做的事情,我知道当我知道最新的选择时我需要做更多的事情,但那会在以后发生。
from Tkinter import *
class GUI():
def __init__(self,frame): # Some Init
self.listbox = Listbox(root, height=20, width=51, selectmode=MULTIPLE, exportselection=0, yscrollcommand=yscrollbar.set, xscrollcommand=xscrollbar.set)
# -- Some Grid setup here --
self.listbox.bind("<<ListboxSelect>>", self.selectionCallback)
frame.bind("<Shift_L>", self.shiftCallback)
frame.bind("<KeyRelease-Shift_L>", self.shiftCallback)
def selectionCallback(self,event):
print self.listbox.get(ACTIVE) # This is where im stuck
def shiftCallback(self,event):
if event.type is 2: #KeyPress
self.shift = True
elif event.type is 3: #KeyRelease
self.shift = False
if __name__ …Run Code Online (Sandbox Code Playgroud) 简单的问题是我可以使用其他函数而不是println,因为我想将非静态变量输出到文件usig out.println();
这是我的代码:
import java.io.*;
public class main {
String outputString ="Math.sqrt(25);" ;
static String outputPath ="src/output.txt";
/**
* @param args
*/
public static void main(String[] args) throws IOException {
File f;
f= new File (outputPath);
//file creation
if(!f.exists()){
f.createNewFile();
System.out.println("File has been created");
}else{
f.delete();
System.out.println("1. File has been deleted");
f.createNewFile();
System.out.println("2. File has been created");
}
//adding string(text) to file
try{
FileWriter outFile = new FileWriter(args[0]);
PrintWriter out = new PrintWriter(outFile);
out.println(outputString);
out.close();
}catch(IOException e){
e.printStackTrace();
}
} …Run Code Online (Sandbox Code Playgroud)