这是我第一次在这里问一个问题所以请耐心等待。我正在尝试创建一个GUI,将任何新的/更新的文本文件从一个文件传输到另一个文件。 GUI还应显示上次文件传输的日期作为参考。我的主要问题是我的抓取文件的功能会自动运行而不是按下按钮。我该怎么做才能将它固定到按下相应按钮时它们只运行的位置。
import sqlite3
from tkinter import *
from tkinter import filedialog
import tkinter as tk
import datetime
from datetime import datetime, timedelta
import os
import shutil
databaseName = 'last_check.sqlite'
#make table
def datetime_tbl():
conn = sqlite3.connect(databaseName)
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS datetime_tbl(datestamp TEXT)');
c.close()
conn.close()
#grab entry from ui
def data_entry():
conn = sqlite3.connect(databaseName)
c = conn.cursor()
c.execute("INSERT INTO datetime_tbl (datestamp) VALUES (?)",
(str(datetime.now()),))
conn.commit()
conn.close()
#this grabs file transfer from tbl for entry form
def generatelasttransfer():
conn = sqlite3.connect(databaseName)
c = conn.cursor()
global transfer
transfer = c.execute("""SELECT MAX(datestamp) FROM datetime_tbl ORDER BY
datestamp DESC LIMIT 1""").fetchall()
#print(transfer)#testing
return(transfer)
c.close()
#conn.close()
def ui(root):
transfer = StringVar()
transfer.set(generatelasttransfer())
src_filename = StringVar()
src_filename.set(src_files())
des_filename = StringVar()
des_filename.set(des_files())
#btn for main file transfer
btn_ck = tk.Button(text='File Transfer',command= f_transfer).grid(row =
7, column = 2, padx = 5, pady = 5)
#btns for src and des files
btn_src = tk.Button(text='Choose file to send out',command=
src_files).grid(row = 2, column = 2, padx = 5, pady = 5)
btn_des = tk.Button(text='Choose file to send to',command=
des_files).grid(row = 2, column = 4, padx = 5, pady = 5)
tk.Entry(text= src_filename).grid(row = 3, column = 2, padx = 15, pady = 5)
tk.Entry(text= des_filename).grid(row = 3, column = 4, padx = 15, pady = 5)
tk.Entry(root, textvariable = transfer, width=28).grid(row = 7,column = 4,padx = 7,pady = 7)
def src_files():
global src_filename
src_filename = filedialog.askdirectory()
return src_filename
def des_files():
global des_filename
des_filename = filedialog.askdirectory()
return des_filename
#file transfer
def f_transfer():
for root,dirs,files in os.walk(src_filename):
for file_name in files:
now = datetime.now()
before = now - timedelta(hours=24)
files = os.path.join(src_filename, file_name)
mod_time = datetime.fromtimestamp(os.path.getmtime(files))
if mod_time > before:
shutil.move(os.path.join(src_filename, file_name),
des_filename)
data_entry()
if __name__ == '__main__':
datetime_tbl()
root = Tk()
root.mainloop
ui(root)
答案 0 :(得分:1)
闭包非常方便,因为此处的数据容器(src_filename
,dest_filename
)是可变的StringVar
个对象。
首先,src_files()
和des_files()
可以重构为单个函数。让我们失去对那些全局变量的使用。
def get_file(src_filename):
def wrap():
src_filename.set(filedialog.askdirectory())
return wrap
这将作为按钮命令传递:
btn_src = tk.Button(
text='Choose file to send out',
command=get_file(src_filename)
).grid(
row=2, column=2, padx=5, pady=5
)
btn_des = tk.Button(
text='Choose file to send to',
command=get_file(des_filename)
).grid(row=2, column=4, padx=5, pady=5)
f_transfer
也将关闭src_filename
和des_filename
,因为它使用这两个名称。
def f_transfer(src_filename, des_filename):
def wrap():
_src_filename, _des_filename = src_filename.get(), des_filename.get()
for root,dirs,files in os.walk(_src_filename):
for file_name in files:
now = datetime.now()
before = now - timedelta(hours=24)
files = os.path.join(_src_filename, file_name)
mod_time = datetime.fromtimestamp(os.path.getmtime(files))
if mod_time > before:
shutil.move(
os.path.join(_src_filename, file_name),
_des_filename
)
data_entry()
return wrap
我们将这两个名称绑定到f_transfer
,然后将其作为btn_ck
的命令传递。
btn_ck = tk.Button(
text='File Transfer',
command=f_transfer(src_filename, des_filename)
).grid(
row=7, column=2, padx=5, pady=5
)
奖金:我们需要在设置GUI组件之后运行mainloop et Voila!
if __name__ == '__main__':
datetime_tbl()
root = Tk()
ui(root)
root.mainloop()