簡體   English   中英

Tkinter 保存在文本中

[英]Tkinter Save In Text

如果有任何問題,我會嘗試讓用戶進行更改,並且當按下保存按鈕時,他的操作將被保存。 我嘗試了一些代碼,但沒有奏效。 我需要做什么shell?

import tkinter
window = tkinter.Tk()
window.geometry("500x500")
window.title('Hatırlatıcı')

def write():
    text = et.get()
    file_one = open('jobs.txt', 'a')
    file_one.write('{}'.format(text))
    file_one.write('\n')
    file_one.close()

def read():
    file_open = open('jobs.txt', 'r')
    if file_open.mode == 'r':
        contents = file_open.read()
    tarea.insert(INSERT, contents)
    file_open.close()

def al():
    write()
    read()

def clear():
    tarea.delete('1.0', END)

def save():
    new = tarea.get('1.0', END)
    contents = new

lb1 = Label(window, text='What Did You Do?', fg='red', font=("Times", 14, "bold"), cursor='tcross', justify='center')
et = Entry(font=("Comic Sans MS", 10, "bold"))
b1 = Button(text='Confirm', command=al)
b2 = Button(text='See Your Progress', command=read)
b3 = Button(text='Clear', command=clear)
b4 = Button(text='Save', command=save)
tarea = Text(width='50')
lb1.pack()
et.pack()
b1.pack()
b2.pack()
b3.pack()
b4.pack()
tarea.pack()
et.place(x='30',y='65')
b1.place(x='220',y='65')
b2.place(x='270',y='110')
b3.place(x='400',y='110')
b4.place(x='200',y='110')
tarea.place(x='45',y='150')
window.mainloop()

沒有錯誤但不工作

將前兩行更改為:

from tkinter import *
window = Tk()

since you used the code import tkinter , you need to write tkinter before each function you use from tkinter, that was the mistake at line 31, you called Label like this:

lb1 = Label(window, text='What Did You Do?', fg='red', font=("Times", 14, "bold"), cursor='tcross', justify='center')

,而您應該這樣稱呼它:

lb1 = tkinter.Label(window, text='What Did You Do?', fg='red', font=("Times", 14, "bold"), cursor='tcross', justify='center')

或者,您可以使用 tkinter import * 中的代碼from tkinter import *這就是我更喜歡做的事情,這就是我如何更改代碼的前兩行。

所以現在你不需要在它的功能之前參考 tkinter 並且你的代碼現在可以正常工作了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM