[英]How to respond to tkinter events?
I'm doing some work with GUI in python. 我正在使用python中的GUI做一些工作。 I'm using the Tkinter library.
我正在使用Tkinter库。
I need a button, which will open a .txt file and do this bit of processing: 我需要一个按钮,它将打开一个.txt文件并进行以下处理:
frequencies = collections.defaultdict(int) # <-----------------------
with open("test.txt") as f_in:
for line in f_in:
for char in line:
frequencies[char] += 1
total = float(sum(frequencies.values())) #<-------------------------
I started with: 我开始于:
from Tkinter import *
import tkFileDialog,Tkconstants,collections
root = Tk()
root.title("TEST")
root.geometry("800x600")
button_opt = {'fill': Tkconstants.BOTH, 'padx': 66, 'pady': 5}
fileName = ''
def openFile():
fileName = tkFileDialog.askopenfile(parent=root,title="Open .txt file", filetypes=[("txt file",".txt"),("All files",".*")])
Button(root, text = 'Open .txt file', fg = 'black', command= openFile).pack(**button_opt)
frequencies = collections.defaultdict(int) # <-----------------------
with open("test.txt") as f_in:
for line in f_in:
for char in line:
frequencies[char] += 1
total = float(sum(frequencies.values())) #<-------------------------
root.mainloop()
Now I don't know how to assemble my code so it runs when the button is pressed. 现在,我不知道如何汇编代码,因此当按下按钮时它就可以运行。
The main problem was tkFileDialog.askopenfile()
returns an open file
rather than a file name. 主要问题是
tkFileDialog.askopenfile()
返回打开的file
而不是文件名。 This following seemed to be more-or-less working for me: 以下内容似乎为我工作:
from Tkinter import *
import tkFileDialog, Tkconstants,collections
root = Tk()
root.title("TEST")
root.geometry("800x600")
def openFile():
f_in = tkFileDialog.askopenfile(
parent=root,
title="Open .txt file",
filetypes=[("txt file",".txt"),("All files",".*")])
frequencies = collections.defaultdict(int)
for line in f_in:
for char in line:
frequencies[char] += 1
f_in.close()
total = float(sum(frequencies.values()))
print 'total:', total
button_opt = {'fill': Tkconstants.BOTH, 'padx': 66, 'pady': 5}
fileName = ''
Button(root, text = 'Open .txt file',
fg = 'black',
command= openFile).pack(**button_opt)
root.mainloop()
For quickly creating simple GUI programs I highly recommend EasyGUI , a fairly powerful yet simple Tk
--based Python module for doing such things. 为了快速创建简单的GUI程序,我强烈建议EasyGUI ,这是一个功能强大但非常简单的基于
Tk
Python模块,可用于执行此类操作。
Try something laid out a bit like this: 试试这样的布局:
class my_app():
def __init__():
self.hi_there = Tkinter.Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=Tkinter.LEFT)
def say_hi():
# do stuff
You also may want to read: 您可能还需要阅读:
This tutorial on Tkinter, 关于Tkinter的本教程
EDIT: The OP wanted an example with his code (I think) so here it is: 编辑: OP想要一个带有他的代码的示例(我认为),因此它是:
from Tkinter import *
import tkFileDialog,Tkconstants,collections
class my_app:
def __init__(self, master):
frame = Tkinter.Frame(master)
frame.pack()
self.button_opt = {'fill': Tkconstants.BOTH, 'padx': 66, 'pady': 5}
self.button = Button(frame, text = 'Open .txt file', fg = 'black', command= self.openFile).pack(**button_opt)
self.calc_button = Button(frame, text = 'Calculate', fg = 'black', command= self.calculate).pack()
self.fileName = ''
def openFile():
fileName = tkFileDialog.askopenfile(parent=root,title="Open .txt file", filetypes=[("txt file",".txt"),("All files",".*")])
def calculate():
############################################### *See note
frequencies = collections.defaultdict(int) # <-----------------------
with open("test.txt") as f_in:
for line in f_in:
for char in line:
frequencies[char] += 1
total = float(sum(frequencies.values())) #<-------------------------
################################################
root = Tk()
app = App(root)
root.title("TEST")
root.geometry("800x600")
root.mainloop()
*Note: Nowhere in your code did I see where collections came from so I wasn't quite sure what to do with that block. *注意:在您的代码中没有任何地方可以看到集合的来源,因此我不确定该块的用途。 In this example I have set it to run on the
在此示例中,我将其设置为在
在您的openFile()
函数中,在您询问用户文件名之后,放上您的代码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.