Python 如何应对tkinter事件?
我正在python中使用GUI进行一些工作。我在用特金特图书馆 我需要一个按钮,它将打开一个.txt文件并进行以下处理:Python 如何应对tkinter事件?,python,button,tkinter,Python,Button,Tkinter,我正在python中使用GUI进行一些工作。我在用特金特图书馆 我需要一个按钮,它将打开一个.txt文件并进行以下处理: frequencies = collections.defaultdict(int) # <----------------------- with open("test.txt") as f_in: for line in f_in: for char in line: freq
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())) #<-------------------------
frequencies=collections.defaultdict(int)#在您的openFile()
函数中,在您向用户询问文件名后,将代码放入 试试这样的布局:
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
您可能还想阅读:
编辑: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()
从Tkinter导入*
导入tkFileDialog、Tkconstants、集合
分类我的应用程序:
定义初始(自我,主):
帧=Tkinter.frame(主帧)
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\u opt)
self.calc_button=按钮(框架,文本='Calculate',fg='black',命令=self.Calculate).pack()
self.fileName=“”
def openFile():
fileName=tkFileDialog.askopenfile(父项=root,title=“Open.txt file”,文件类型=[(“txt文件”、“.txt”),(“所有文件”、“*”))
def calculate():
###############################################*见附注
frequencies=collections.defaultdict(int)#主要问题是tkFileDialog.askopenfile()
返回打开的文件而不是文件名。以下内容似乎或多或少对我有用:
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()
为了快速创建简单的GUI程序,我强烈推荐一个功能相当强大但简单的基于Tk
的Python模块来完成这些工作。这不起作用,我尝试了这个,但当我运行它时,首先打开窗口对话框,打开text.file。我想要一个按钮,然后点击它并打开.txt文件,然后按照“代码”中的内容来做。谢谢你的帮助,但我仍然无法让它工作。你们能复制一下你们的例子的代码吗?快3分钟+答案很好,比我的答案要简单得多。
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()