我已经写了这段代码,我知道它可以重新格式化,以便看起来更干净,但我不知道如何。谁能帮帮我?
I have written this piece of code and I know it can be reformatted, in order to look cleaner, but I don't know how. Could anyone help me?
我是编程新手,我想了解一些有关如何使其更简洁的见解。这是每月费用计划的一部分。
1
import tkinter as tk
from tkinter import ttk
class JanelaDespesa(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.geometry('300x100')
self.title('Despesa')
ttk.Button(self,
text='Fechar',
command=self.destroy).pack(expand=True)
class JanelaReceita(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.geometry('300x100')
self.title('Receita')
ttk.Button(self,
text='Fechar',
command=self.destroy).pack(expand=True)
class JanelaRelatorio(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.geometry('300x100')
self.title('Relatório')
ttk.Button(self,
text='Fechar',
command=self.destroy).pack(expand=True)
class JanelaModificar(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.geometry('300x100')
self.title('Modificar Dados')
ttk.Button(self,
text='Fechar',
command=self.destroy).pack(expand=True)
class TelaPrincipal(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('700x200')
self.title('Tela Principal')
ttk.Button(self,
text='Lançar Receita',
command=self.open_window1).pack(expand=True)
ttk.Button(self,
text='Lançar Despesa',
command=self.open_window2).pack(expand=True)
ttk.Button(self,
text='Relatório',
command=self.open_window3).pack(expand=True)
ttk.Button(self,
text='Modificar Dado',
command=self.open_window4).pack(expand=True)
def open_window1(self):
window = JanelaReceita(self)
window.grab_set()
def open_window2(self):
window = JanelaDespesa(self)
window.grab_set()
def open_window3(self):
window = JanelaRelatorio(self)
window.grab_set()
def open_window4(self):
window = JanelaModificar(self)
window.grab_set()
app = TelaPrincipal()
app.mainloop()
您编写的代码比实际需要的多得多。
考虑一下:
import tkinter as tk
from tkinter import ttk
from functools import partial
class Janela(tk.Toplevel):
def __init__(self, parent, title, geometry='300x100'):
super(Janela, self).__init__(parent)
self.geometry(geometry)
self.title(title)
button = ttk.Button(self, text='Fechar', command=self.destroy)
button.pack(expand=True)
class TelaPrincipal(tk.Tk):
def __init__(self):
super(TelaPrincipal, self).__init__()
self.geometry('700x200')
self.title('Tela Principal')
control = [
('Lançar Receita', 'Receita'),
('Lançar Despesa', 'Despesa'),
('Relatório', 'Relatorio'),
('Modificar Dado', 'Modificar')]
for text, title in control:
ttk.Button(self, text=text, command=partial(self.open_window, title)).pack(expand=True)
def open_window(self, title):
Janela(self, title).grab_set()
app = TelaPrincipal()
app.mainloop()
我是编程新手,我想了解一些有关如何使其更简洁的见解。这是每月费用计划的一部分。 1
import tkinter as tk
from tkinter import ttk
class JanelaDespesa(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.geometry('300x100')
self.title('Despesa')
ttk.Button(self,
text='Fechar',
command=self.destroy).pack(expand=True)
class JanelaReceita(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.geometry('300x100')
self.title('Receita')
ttk.Button(self,
text='Fechar',
command=self.destroy).pack(expand=True)
class JanelaRelatorio(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.geometry('300x100')
self.title('Relatório')
ttk.Button(self,
text='Fechar',
command=self.destroy).pack(expand=True)
class JanelaModificar(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.geometry('300x100')
self.title('Modificar Dados')
ttk.Button(self,
text='Fechar',
command=self.destroy).pack(expand=True)
class TelaPrincipal(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('700x200')
self.title('Tela Principal')
ttk.Button(self,
text='Lançar Receita',
command=self.open_window1).pack(expand=True)
ttk.Button(self,
text='Lançar Despesa',
command=self.open_window2).pack(expand=True)
ttk.Button(self,
text='Relatório',
command=self.open_window3).pack(expand=True)
ttk.Button(self,
text='Modificar Dado',
command=self.open_window4).pack(expand=True)
def open_window1(self):
window = JanelaReceita(self)
window.grab_set()
def open_window2(self):
window = JanelaDespesa(self)
window.grab_set()
def open_window3(self):
window = JanelaRelatorio(self)
window.grab_set()
def open_window4(self):
window = JanelaModificar(self)
window.grab_set()
app = TelaPrincipal()
app.mainloop()
您编写的代码比实际需要的多得多。
考虑一下:
import tkinter as tk
from tkinter import ttk
from functools import partial
class Janela(tk.Toplevel):
def __init__(self, parent, title, geometry='300x100'):
super(Janela, self).__init__(parent)
self.geometry(geometry)
self.title(title)
button = ttk.Button(self, text='Fechar', command=self.destroy)
button.pack(expand=True)
class TelaPrincipal(tk.Tk):
def __init__(self):
super(TelaPrincipal, self).__init__()
self.geometry('700x200')
self.title('Tela Principal')
control = [
('Lançar Receita', 'Receita'),
('Lançar Despesa', 'Despesa'),
('Relatório', 'Relatorio'),
('Modificar Dado', 'Modificar')]
for text, title in control:
ttk.Button(self, text=text, command=partial(self.open_window, title)).pack(expand=True)
def open_window(self, title):
Janela(self, title).grab_set()
app = TelaPrincipal()
app.mainloop()