【发布时间】:2021-06-04 22:33:58
【问题描述】:
我有一个文本文件,其中包含需要在滚动框 tkinter 中显示的值。我已经成功地做到了这一点,但有一个问题。当我按下提交向程序中的文本文件添加更多值时,显示文本文件内容的滚动框不会更新为新值。
这是我的文本文件:
amazon.com
username@gmail.com
n3*F/X"qse~a`_+
netflix.com
username@outlook.com
avOmCl|Jb?O<
apple.com
username@icloud.com
s=DT8n*Y"y
但是,当我按下提交时,文本文件会使用提交的值更新,但滚动框不会。
这是我的完整代码:
import string
import random
from tkinter import *
import tkinter.scrolledtext as st
mypasslist = []
with open("password&usernames.txt", "r") as input:
for line in input:
items = line.split()
mypasslist.append([item for item in items[0:]])
def generatePassword():
characters = string.ascii_letters + string.punctuation + string.digits
password = "".join(random.choice(characters) for x in range(random.randint(8, 16)))
return password
def writeToFile(input_variable):
with open("password&usernames.txt", "a") as input:
input.writelines(input_variable)
top = Tk()
top.geometry("1920x1080")
def ifSubmit():
global a
a = generatePassword()
label1 = Label(top, text='Your password is: %s' % a, font=("Arial", 11)).place(x = 40, y = 170)
label1 = Label(top, justify='left', text='Your password and username has been saved, you can access them below.').place(x = 40, y = 227)
copy_button = Button(top, text = 'Copy', command=copyText).place(x=40, y=195)
service = service_var.get()
username = username_var.get()
writeToFile('%s\n' % str(service))
writeToFile('%s\n' % str(username))
writeToFile('%s\n' % str(a))
writeToFile('\n')
def copyText():
top.clipboard_clear()
top.clipboard_append(a)
top.update()
# the label for user_name
Service = Label(top, text = "Service").place(x = 40, y = 60)
user_name = Label(top, text = "Username").place(x = 40, y = 100)
submit_button = Button(top, text = "Submit", command=ifSubmit)
submit_button.place(x = 40, y = 130)
service_var = StringVar()
Service_input_area = Entry(top, width = 30, textvariable=service_var)
Service_input_area.place(x = 110, y = 60)
service = service_var.get()
username_var = StringVar()
user_name_input_area = Entry(top, width = 30, textvariable=username_var)
username = username_var.get()
user_name_input_area.place(x = 110, y = 100)
text_area = st.ScrolledText(top, width = 50, height = 10)
text_area.grid(column = 0, pady = 260, padx = 40)
for i in mypasslist:
text_area.insert(INSERT, i)
text_area.insert(INSERT, '\n')
text_area.configure(state ='disabled')
top.mainloop()
那么我怎样才能在每次提交新值时更新程序呢?
【问题讨论】:
-
您希望它在何时何地更新。
标签: python loops tkinter while-loop text-files