【问题标题】:StringVar not writing to file help python 3 tkinterStringVar没有写入文件帮助python 3 tkinter
【发布时间】:2020-01-04 22:02:38
【问题描述】:
import tkinter as tk 
from tkinter import Button
from tkinter import Entry 
from tkinter import Label
from tkinter import StringVar

root = tk.Tk()
root.title("Tech organizer 1.0")
root.geometry ("230x113")
root.configure(background= "lightblue")

def savebt():
    hyee = hye.get()
    hyeee= str(hyee)
    important = open("file.txt","w")
    important . write (hyeee)
    important . close ()


def plc():

    def savebt ():
        hyee = hye.get()
        hyeee= str(hyee)
        important = open("file.txt","w")
        important . write (hyeee)
        important . close ()
        print(hye.get())


    plca = tk.Tk()
    plca.title ("pc & laptop components")
    plca.geometry ("850x560")
    plca.configure(background= "lightgrey")
    global itemlocation
    global itemlocation
    global snpn
    global var_1
    global item 
    global hye
    hye = StringVar()




    Entry (plca , textvariable = hye).grid(row = 5 , column = 5)
    #Entry (plca,textvariable = seralnumberpartnumber1, width =         30).grid(row = 10,column = 11)
    #Entry(plca,textvariable = itemlocation1, width = 30 ).grid(row = 10, column = 12)
    Button (plca, text= "save", command = savebt).grid(row = 11, column= 12)

def lsl():
    lsla = tk.Tk()
    lsla.title ("laptop storage location")
    lsla. geometry ("850x560")
    def item ():
        pass


    def ane ():
        pass

    def laptopmodel():
        pass

    def storedlocation():
        pass

    Entry(lsla, textvariable = item ).grid(row = 2, column = 2)

    Entry(lsla,textvariable = laptopmodel, width = 30).grid(row = 2, column = 3)

    Entry(lsla, textvariable = storedlocation, width = 30).grid(row = 2, column = 4)

    Button(lsla, text = "Add New Entry", command = ane).grid(row = 2,column = 5)





def psl():
    psla = tk.Tk()
    psla.title ("phone storage location")
    psla.geometry ("850x560")
    psla.configure(background= "lightgrey")
    def ane ():
    pass 



    def item ():
        pass


    Button (psla,text = "Add Entry",command = ane).grid(row=3,column=4)

    Entry (psla ,textvariable = item).grid(row=2,column=3)






def otsl():
    otsla = tk.Tk()
    otsla.title ("other tech stuff location")
    otsla.geometry ("850x560")
    otsla.configure(background= "lightgrey")

    def ane ():
        pass

    Button(otsla, text= "Add New Entry", command = ane)






Button (root,text="pc & laptop components",command = plc).grid(row=1,column=4)
Button (root,text="laptop storage location", command= lsl ).grid(row=2,column= 4)
Button (root, text="phone storagelocation",command= psl).grid(row=3,column=4)
Button (root, text= "other tech stuff location",command = otsl).grid(row=4,column=4)
root.mainloop()

我正在以初学者的身份编写此代码,而我遇到的问题是 def plc。因此,当您运行它时,您需要单击第一个按钮并在框中输入内容,然后单击保存。单击保存后,您应该检查它是否有一个名为“file.txt”的文件,然后当您打开它时,您应该查看您在框中写的内容是否存在

【问题讨论】:

  • 我也在用python 3.5

标签: python-3.x file tkinter


【解决方案1】:

定义您的 Entry 小部件,如:

entry = Entry (plca , textvariable = hye)
entry.grid(row = 5 , column = 5)

之后要从小部件条目中获取当前文本,您需要在该小部件上使用 get 函数:

def savebt ():
    hyee = entry.get() # use get function on widget not variable
    hyeee= str(hyee)
    important = open("file.txt","w")
    important.write (hyeee)
    important.close()

【讨论】:

  • 非常感谢,我已经尝试了 2 天。成功了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 2011-12-13
  • 1970-01-01
相关资源
最近更新 更多