2017-10-10 76 views
-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) 

回答

1

閉包是非常方便的在這裏,因爲這裏的數據容器(src_filenamedest_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 

我們將會把它當作命令btn_ck前兩個名字f_transfer綁定。

btn_ck = tk.Button(
    text='File Transfer', 
    command=f_transfer(src_filename, des_filename) 
).grid(
    row=7, column=2, padx=5, pady=5 
) 

獎金:我們需要建立的GUI組件等瞧後運行主循環!

if __name__ == '__main__': 
    datetime_tbl() 
    root = Tk() 
    ui(root) 
    root.mainloop() 
+0

真棒現在它完美的作品!非常感謝! – Manus