gpt4 book ai didi

Python:使用 Tkinter 单选按钮从 GUI 添加/删除字段

转载 作者:太空宇宙 更新时间:2023-11-04 05:57:45 25 4
gpt4 key购买 nike

我目前正在研究一个基于 GUI 的 (Tkinter) 项目,用于减少矩阵,仅供练习。在深入研究实际的数学知识之前,我正在研究一个基本窗口,以询问用户信息以格式化他们的矩阵。

在一个字段中,我有单选按钮 - 一个是“分数”,另一个是“小数” - 他们可以选择其中一种作为输出格式。我想要的是,如果用户选择“小数”,另一个输入字段将出现在下方,以便他们可以输入要四舍五入的小数位数,如果他们选择“分数”,则该字段不会出现。

我在 Stack Overflow 上搜索了一下,看到了一些关于使用“raise()”和“lower()”方法的信息,但它现在不起作用。如果你们能提供任何意见,那就太好了。关于 GUI 的一般反馈也很受欢迎 - 我前段时间在学校用 Python 学习了 GUI,所以如果格式不好,请告诉我!

提前致谢!

编辑:我正在运行 Python 3.4。

import tkinter

class Reducer:
'''
Takes user input of a matrix and row-reduces it to RREF.

GUI-Based interface that asks the user for the # of rows and columns, as well as output formatting.
The program can output as a fraction or a decimal with a specified number of decimals.
'''


def __init__(self):
'''
Initializes the GUI interface that asks the user for input.

No parameters or returns.
'''

self.main_window = tkinter.Tk()

#Initalize frames.
self.info_frame = tkinter.Frame()
self.size_frame = tkinter.Frame()
self.format_frame = tkinter.Frame()
self.digit_frame = tkinter.Frame()
self.button_frame = tkinter.Frame()

#Object for the information frame.
self.info_label = tkinter.Label(self.info_frame, text="Enter the number of rows and columns, your preferred output format,"
" and the number of trailing decimals if applicable.", justify="left",
wraplength=275).pack()

#Objects for the size frame - the matrix's rows/columns are set here.
self.row_label = tkinter.Label(self.size_frame, text="Rows:").pack(side="left")
self.row = tkinter.Entry(self.size_frame, width=3).pack(side="left")
self.col_label = tkinter.Label(self.size_frame, text="Columns:").pack(side="left")
self.col = tkinter.Entry(self.size_frame, width=3).pack(side="left")

#Objects for the digit frame
self.digit_label = tkinter.Label(self.digit_frame, text="Digits:")
self.digit = tkinter.Entry(self.digit_frame, width=3)
self.digit_label.pack(side="left")
self.digit.pack(side="left")

#Objects for the format frame - the output formatting is specified here.
self.output_var = tkinter.IntVar()
self.output_var.set(0)

self.fraction = tkinter.Radiobutton(self.format_frame, text="Fraction", variable=self.output_var, value=0,
command=self.hide_digits()).pack(side="left")

self.decimal = tkinter.Radiobutton(self.format_frame, text="Decimal", variable=self.output_var, value=1,
command=self.show_digits()).pack(side="left")

#Object for the bottom frame

self.button = tkinter.Button(self.button_frame, text="Next", command=self.reduce).pack()

#Pack frames.
self.info_frame.pack(anchor="nw")
self.size_frame.pack(anchor="nw")
self.format_frame.pack(anchor="nw")
self.digit_frame.pack(anchor="nw")
self.button_frame.pack(anchor="nw")

tkinter.mainloop()

def show_digits(self):
self.digit_label.lift(self.col)
self.digit.lift(self.col)

def hide_digits(self):
self.digit_label.lower(self.col)
self.digit.lower(self.col)

def reduce(self):
pass



reducer = Reducer()

最佳答案

这是我之前提到的一个例子!这会创建一个新的输入字段(任何时候一个)。

import tkinter as tk

root = tk.Tk()

global num
num = 0

def create():
global entry
global num
if num == 0:
num = 1
entry = tk.Entry()
entry.pack()

def destroy():
global entry
global num
if num == 1:
num = 0
entry.destroy()

button1 = tk.Button(text = 'create entry', command = create)
button1.pack()

button2 = tk.Button(text = 'destroy entry', command = destroy)
button2.pack()

root.mainloop()

您也可以像这样禁用和启用输入字段,但是小部件始终存在,所以由您决定:

import tkinter as tk

root = tk.Tk()

global entry
entry = tk.Entry(state = 'disabled')
entry.pack()

def create():
global entry
entry.config(state = 'normal')


def destroy():
global entry
entry.config(state = 'disabled')

button1 = tk.Button(text = 'create entry', command = create)
button1.pack()

button2 = tk.Button(text = 'destroy entry', command = destroy)
button2.pack()

root.mainloop()

希望对您有所帮助。

关于Python:使用 Tkinter 单选按钮从 GUI 添加/删除字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26821843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com