我应该如何读写 TkInter 的配置文件? [英] How should I read and write a configuration file for TkInter?

查看:71
本文介绍了我应该如何读写 TkInter 的配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个配置文件中收集了数字,我想将它们应用到按钮上.单击按钮应该允许更改数字,然后重新写入配置文件.我目前的代码如下:

I've gathered numbers in a configuration file, and I would like to apply them to buttons. Clicking the button should allow the number to be changed and then re-written to the config file. My current code is as follows:

from tkinter import*
import tkinter as tk
import tkinter.simpledialog

def onChange(i):     
    btn_list[i].config(text='Updating...',bg='red')  
    btn_list[i].grid(in_=root,row=rw[i],column=2)
    ans=tk.simpledialog.askfloat('Updating....', 'What is the current price?')
    if ans:
        btn_list[i].config(text='RM {:,.2f}'.format(ans))
        btn_list[i].config(bg='yellow')
        c=str(ans)
        fw=open('dataUpdate.txt','w')
        fw.write(c)
        fw.close()

root=Tk()

Title=['Item','Unit','Price']
Item=['Kopi O','Teh O','Teh Tarik']
Unit= '1 cup'
cl=[0,1,2]
rw=[1,2,3]
btn_list=[]
fr=open('dataUpdate.txt','r')

with open('dataUpdate.txt') as input_file:
    text=input_file.read()
    strings=text.split()
    number=[float(item) for item in strings]
    print(number)
    fr.close()

for k in range(3):
    btnT1=tk.Button(root,text=Title[k],width=12,bg='light green')
    btnT1.grid(in_=root,row=0,column=cl[k])

for x in range(3):
    btnT2=tk.Button(root,text=Item[x],width=12)
    btnT2.grid(in_=root,row=rw[x],column=0)

for y in range(3):
    btnT3=tk.Button(root,text=Unit,width=12)
    btnT3.grid(in_=root,row=rw[y],column=1)             

for z in range(3):
    btnT4=tk.Button(root,text=('RM {:,.2f}'.format(number[z])),bg='yellow',width=12,\
                command=lambda i=z:onChange(i))
    btnT4.grid(in_=root,row=rw[z],column=2)
    btn_list.append(btnT4)

root.mainloop()

这是我的基本配置文件的屏幕截图:

Here's a screenshot of my basic configuration file:

推荐答案

试试这个简单的例子:

from tkinter import*
import tkinter as tk
import tkinter.simpledialog
import configparser
import os.path

def onChange(i):     
    btn_list[i].config(text='Updating...',bg='red')  
    btn_list[i].grid(in_=root,row=rw[i],column=2)
    ans=tk.simpledialog.askfloat('Updating....', 'What is the current price?')
    if ans:
        btn_list[i].config(text='RM {:,.2f}'.format(ans))
        btn_list[i].config(bg='yellow')
        c=str(ans)
        #fw=open('dataUpdate.txt','w')
        #fw.write(c)
        #fw.close()
        #----------------------------------------------
        # Here you can call update(section, key value) 
        update('Section1', 'number%s' % i, c)
        #----------------------------------------------


root=Tk()

Title=['Item','Unit','Price']
Item=['Kopi O','Teh O','Teh Tarik']
Unit= '1 cup'
cl=[0,1,2]
rw=[1,2,3]
btn_list=[]

#------------------------------------------------------------------------
config = configparser.RawConfigParser()

def init():
    'Create a configuration file if does not exist'
    config.add_section('Section1')
    config.set('Section1', 'number1', '1')
    config.set('Section1', 'number2', '0.8')
    config.set('Section1', 'number3', '0.2')
    with open('dataUpdate.cfg', 'w') as output:
        config.write(output)

#------------------------------------------------------------------------
# check if dataUpdate.cfg exist if not create it   
if not os.path.exists('dataUpdate.cfg'):
    init()

# Read configurations using section and key to get the value
config.read('dataUpdate.cfg')
number = [config.getfloat('Section1', 'number%s' % (i)) for i in range(3)]
#------------------------------------------------------------------------




#------------------------------------------------------------
def update(section, key, value):
    #Update config using section key and the value to change
    #call this when you want to update a value in configuation file
    # with some changes you can save many values in many sections
    config.set(section, key, value )
    with open('dataUpdate.cfg', 'w') as output:
        config.write(output)
#------------------------------------------------------------


for k in range(3):
    btnT1=tk.Button(root,text=Title[k],width=12,bg='light green')
    btnT1.grid(in_=root,row=0,column=cl[k])

for x in range(3):
    btnT2=tk.Button(root,text=Item[x],width=12)
    btnT2.grid(in_=root,row=rw[x],column=0)

for y in range(3):
    btnT3=tk.Button(root,text=Unit,width=12)
    btnT3.grid(in_=root,row=rw[y],column=1)             

for z in range(3):
    btnT4=tk.Button(root,text=('RM {:,.2f}'.format(number[z])),bg='yellow',width=12,\
                command=lambda i=z:onChange(i))
    btnT4.grid(in_=root,row=rw[z],column=2)
    btn_list.append(btnT4)

root.mainloop()

这篇关于我应该如何读写 TkInter 的配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆