python 我如何在这个Tkinter问题中制定规则?

ulmd4ohb  于 2023-04-04  发布在  Python
关注(0)|答案(1)|浏览(131)

我正在PyCharm中处理这个GUI风格的问题,我运行我的主文件来打开GUI,如屏幕截图所示。

现在,有一些规则我很难遵循,因为这对我来说是新的。

  • 使用异常处理来处理无效的年龄输入。因此,当输入整数以外的任何内容时,它将清除“年龄”值,保持“名称”和“状态”处于选中状态,并要求重新输入年龄。
  • 如果未选择任何状态,请在输出文件的相应部分中放置“未知”。
  • 单击“保存”后,光标将位于“名称”框中。

代码:

from tkinter import *

class GUI:
   def __init__(self, window):

     self.window = window

     self.frame_name = Frame(self.window)

     self.label_name = Label(self.frame_name, text='Name')
     self.entry_name = Entry(self.frame_name)
     self.label_name.pack(padx=5, side='left')
     self.entry_name.pack(padx=5, side='left')
     self.frame_name.pack(anchor='w', pady=10)

     self.frame_age = Frame(self.window)
     self.label_age = Label(self.frame_age, text='Age')
     self.entry_age = Entry(self.frame_age)
     self.label_age.pack(padx=9, side='left')
     self.entry_age.pack(padx=9, side='left')
     self.frame_age.pack(anchor='w', pady=10)

     self.frame_status = Frame(self.window)
     self.label_status = Label(self.frame_status, text='Status')
     self.label_status.pack(padx=5, side='left')

     self.rbtn = StringVar()
     self.rbtn.set('none')
     self.rbutton_student = Radiobutton(self.frame_status,text='Student',variable=self.rbtn,value="Student")
     self.rbutton_student.pack(padx=10, side='left')
     self.rbutton_staff = Radiobutton(self.frame_status,text='Staff',variable=self.rbtn,value="Staff")
     self.rbutton_staff.pack(padx=10, side='left')
     self.rbutton_both = Radiobutton(self.frame_status,text='Both',variable=self.rbtn,value="Both")
     self.rbutton_both.pack(padx=10, side='left')
     self.frame_status.pack(anchor='w', pady=10)

     self.frame_button = Frame(self.window)
     self.button_save = Button(text="Save",command = self.clicked)
     self.button_save.pack(anchor='center')
     self.frame_button.pack(anchor='w',pady=10)

   def clicked(self):
     name = self.entry_name.get()
     age = int(self.entry_age.get())*2
     status = self.rbtn.get()

     with open('data.csv','a') as f:
        f.write(name+","+str(age)+","+status+"\n")
        self.entry_name.delete(0,END)
        self.entry_age.delete(0,END)
        self.rbtn.set('none')
cbeh67ev

cbeh67ev1#

1.这里有一个简单的解决方案,可以防止用户在“年龄”字段中输入非数字值。这也与其他字段无关,因此这些字段将保持不变。这种方法的另一个好处是,您不必强迫用户重新输入文本来惩罚用户不小心输入非数字字符

self.entry_age.bind('<Key>', self.validate_age_input)

# define this method somewhere within your main class, similar to 'clicked'
def validate_age_input(self, event):
    key = event.keysym
    allowed_keys = {'BackSpace', 'Delete', 'Up', 'Down', 'Left', 'Right'}
    # check if the key pressed wasn't a number or a deletion
    if not key.isdigit() or key not in allowed_keys:  
        self.bell()  # warning ding
        return 'break'  # dispose of this event
        # (no text will be entered, any existing text remains in place)

1.您可以简单地将self.rbtn的初始值设置为'Unknown'而不是'none',这样,如果用户在选择“Status”选项之前按下“保存”按钮,您将在输出中自动获得'Unknown'

self.rbtn = StringVar()
self.rbtn.set('Unknown')  # or whatever string you want, really!

1.Side Note -您可以在声明中这样缩短它:

self.rbtn = StringVar(value='Unknown')

1.您可以在clicked()中添加一行,以便再次focus()“Name”条目

def clicked(self):
    name = self.entry_name.get()
    age = int(self.entry_age.get())*2
    status = self.rbtn.get()

    with open('data.csv','a') as f:
        # f.write(name+","+str(age)+","+status+"\n")
        # a more 'Pythonic' way to handle string interpolation like this is
        # using 'f-strings' - check it out:
        f.write(f'{name},{age},{status}\n'
        # note that the 'f' before the string is not the same 'f' as your
        # file reference ;)

    # these lines don't really need to be in the 'with' block
    self.entry_name.delete(0, END)
    self.entry_age.delete(0, END)
    self.rbtn.set('Unknown')  # remember: this is your new 'default' Status value
    self.entry_name.focus()  # return focus to this Entry

相关问题