-1
這是我第一次問就在這裏一個問題如此忍受我的按鈕。我試圖做一個圖形用戶界面,從一個文件傳輸任何新的/更新的文本文件到另一個。 GUI還應顯示上次文件傳輸的日期作爲參考。我的主要問題是,我的函數,搶我的文件上按下按鈕自動運行,而不是。我可以做些什麼來修復它們只在按下相應按鈕時才運行的位置。Python的Tkinter的:通過與變量的函數到調用該函數並傳遞VAR爲參賽表格
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)
真棒現在它完美的作品!非常感謝! – Manus