我无法使用.get()函数将值插入数据库

时间:2019-05-20 12:56:20

标签: python database sqlite tkinter

我正在数据库中创建一个表,我需要将值插入数据库的条目中。自一个星期以来,我一直在尝试各种东西。我是初学者。有人可以找到错误吗?

我尝试了使用变量的各种方法,并使用.get()和?来直接插入值。占位符。

import os
import shutil
import sys


###########################################################

def get_directories(path):
    directories = [x for x in os.listdir(path)]  # 'directories' = x which is in the 'path'
     directories = [(path + x) for x in directories]  # now directories = x that x is x + path
return directories


 ###########################################################
 root = '/Users/tk/Desktop/forPython/sc/'
 path = '/Users/tonggihkang/Desktop/forPython/scott/top_structures/'
 dest = '/Users/tk/Desktop/forPython/sc/ranked/'

 # copy and paste the essential files to the same directories where .xyz located
 _dir = get_directories(dest)  # _dir = /Users/tonggihkang/Desktop/scott/ranked/023 ... Full paths


 ############################################################
 # Produce "trash.sh" with right path for output files
 ############################################################

 dir_1 = [(x.replace(root, '')) for x in _dir]  # dir_1 = ranked/001 ranked/002...  will be placed on the string, 'target_1'
 dir_2 = [(x.replace(dest, '')) for x in _dir]  # dir_1 = 001 002...  will be placed on the string, 'target_2'

 foo = 'trash_1.sh'  # The file for the Reading
 bar = 'trash.sh'  # The file for the writing

 #boo = 'target_1'  # The first target string (which will replaced with dir_1)
 #far = 'target_2'  # The second target string (which will replaced with dir_2)

 for i in range(len(_dir)):
     with open(_dir[i] + '/' + foo, 'r') as f:
         edit_1 = f.read().replace("target_1", str(dir_1).strip('[]'), 1)
         edit_2 = edit_1.replace("target_2", str(dir_2).strip('[]'), 1)
         with open(_dir[i] + '/' + bar, 'w') as f:  # open new file (trash.sh) to copy trash_1.sh down
             f.write(edit_2)
         #fout.write(line.replace('target_1', str(dir_1).strip('[]')).replace('target_2', str(dir_2).strip('[]'))

 os.remove(_dir[i] + '/'+ 'trash_1.sh')

print('"trash_1.sh has been removed and "trash_1.sh" has been produced with right output file path')

错误

  

Tkinter回调中的异常   追溯(最近一次通话):     调用中的文件“ F:\ Python3 \ lib \ tkinter__init __。py”,行1705       返回self.func(* args)     btn_click1中的文件“ F:/ Python Projects / Registration form.py”,第71行       ID = ID.get()   UnboundLocalError:分配前已引用本地变量“ ID”

1 个答案:

答案 0 :(得分:0)

ID = ID.get()将不起作用,因为ID是在stud_win()中定义的局部变量,这意味着您无法从外部访问它。您正在尝试从btn_click1()访问它,这会触发异常。

要解决此问题,必须将其设置为全局变量。同样的情况也适用于btn_click1()

中定义的所有变量