python tkinter button开关_Python Tkinter按钮操作(Python Tkinter Button Action)

Python Tkinter按钮操作(Python Tkinter Button Action)

我有这个代码:

#!/usr/bin/python

import Tkinter

from tkFileDialog import askopenfilename

import tkMessageBox

root = Tkinter.Tk()

def getFileName():

# show an "Open" dialog box.

filename = askopenfilename(filetypes = [('Text files', '*.txt'),('All files','*')])

btnIco = Tkinter.Button(root, text="Icon", command=getFileName())

btnIco.pack()

root.mainloop()

我打算做的是在单击按钮时运行getFileName函数。 但是,当代码运行时,该函数会运行,而单击时按钮不会执行任何操作。 你能指出什么是错的吗?

I have this code:

#!/usr/bin/python

import Tkinter

from tkFileDialog import askopenfilename

import tkMessageBox

root = Tkinter.Tk()

def getFileName():

# show an "Open" dialog box.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用PythonTkinter模块来实现鼠标点击和记录鼠标操作的功能,具体步骤如下: 1. 导入Tkinter模块和time模块。 ```python import tkinter as tk import time ``` 2. 创建一个Tkinter窗口,并设置窗口标题和大小。 ```python win = tk.Tk() win.title("Mouse Recorder and Playback") win.geometry("400x300") ``` 3. 创建一个Canvas对象,并添加到窗口中。 ```python canvas = tk.Canvas(win, bg="white") canvas.pack(expand=True, fill="both") ``` 4. 定义鼠标点击事件的回调函数,并在Canvas上绑定该事件。 ```python def on_click(event): x, y = event.x, event.y canvas.create_oval(x-5, y-5, x+5, y+5, fill="red") actions.append((time.time(), "click", x, y)) canvas.bind("<Button-1>", on_click) ``` 5. 定义记录鼠标操作的函数。 ```python def record(): global recording, actions recording = True actions = [] canvas.delete("all") while recording: win.update() with open("actions.txt", "w") as f: for a in actions: f.write("{},{},{},{}\n".format(a[0], a[1], a[2], a[3])) ``` 6. 定义回放鼠标操作的函数。 ```python def playback(): canvas.delete("all") with open("actions.txt", "r") as f: for line in f: ts, action, x, y = line.strip().split(",") x, y = int(x), int(y) delay = float(ts) - actions[0][0] time.sleep(delay) if action == "click": canvas.create_oval(x-5, y-5, x+5, y+5, fill="blue") canvas.update() ``` 7. 在窗口中添加两个按钮,分别用于开始记录和回放鼠标操作。 ```python record_btn = tk.Button(win, text="Record", command=record) record_btn.pack(side="left") playback_btn = tk.Button(win, text="Playback", command=playback) playback_btn.pack(side="right") ``` 完整代码如下: ```python import tkinter as tk import time win = tk.Tk() win.title("Mouse Recorder and Playback") win.geometry("400x300") canvas = tk.Canvas(win, bg="white") canvas.pack(expand=True, fill="both") def on_click(event): x, y = event.x, event.y canvas.create_oval(x-5, y-5, x+5, y+5, fill="red") actions.append((time.time(), "click", x, y)) canvas.bind("<Button-1>", on_click) def record(): global recording, actions recording = True actions = [] canvas.delete("all") while recording: win.update() with open("actions.txt", "w") as f: for a in actions: f.write("{},{},{},{}\n".format(a[0], a[1], a[2], a[3])) def playback(): canvas.delete("all") with open("actions.txt", "r") as f: for line in f: ts, action, x, y = line.strip().split(",") x, y = int(x), int(y) delay = float(ts) - actions[0][0] time.sleep(delay) if action == "click": canvas.create_oval(x-5, y-5, x+5, y+5, fill="blue") canvas.update() record_btn = tk.Button(win, text="Record", command=record) record_btn.pack(side="left") playback_btn = tk.Button(win, text="Playback", command=playback) playback_btn.pack(side="right") win.mainloop() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值