我正在将应用程序创建为项目。如何在Python的以下代码中使用pack?

时间:2019-03-14 17:32:00

标签: python-3.x user-interface matplotlib

这是一个具有多个屏幕的应用程序,我想根据需要在屏幕上放置小部件,应该使用包装,网格或放置哪种方法?

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figurefrom tkinter import *
Large_Font=("Times New Roman", 12)
month_list = []
amount_list = []
class wholescreen(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        container = Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        for F in (HomeScreen, EntryScreen, GraphScreen, MapScreen):
            frame=F(container, self)
            self.frames[F]=frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(HomeScreen)
    def show_frame(self, cont):
        frame=self.frames[cont]
        frame.tkraise()
class HomeScreen(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        HomeScreen.configure(self, bg="light blue")
        def getInput():
            name = nameText.get()
            address = addressText.get()
            phone=phoneText.get()
            info.append(name)
            info.append(address)
            info.append(phone)
            print(info)
            nameText.delete(0, END)
            addressText.delete(0, END)
            phoneText.delete(0, END)

        mainFrame=(self)
        mainFrame.pack(anchor=NW)
        titleLabel = Label(mainFrame, text="DISEASE TRACKER APP", bg="light blue")
        titleLabel.pack(side=TOP, anchor=N)
        labelFrame=Frame(mainFrame, bg="light blue")
        labelFrame.pack(side=LEFT, anchor=W)
        textFrame=Frame(mainFrame)
        textFrame.pack(side=LEFT, anchor=CENTER)
        buttonFrame=Frame(mainFrame)
        buttonFrame.pack(side=BOTTOM, anchor=SW)
        nameLabel = Label(labelFrame, text="Clinic Name", bg="light blue")
        nameLabel.pack(anchor=W)
        addressLabel = Label(labelFrame, text="Clinic Address", bg="light blue")
        addressLabel.pack(anchor=W)
        phoneLabel = Label(labelFrame, text="Clinic Phone Number", bg="light blue")
        phoneLabel.pack(anchor=W)
        nameText = Entry(textFrame, bg="light blue")
        nameText.pack(anchor=W)
        addressText = Entry(textFrame, bg="light blue")
        addressText.pack(anchor=W)
        phoneText = Entry(textFrame, bg="light blue")
        phoneText.pack(anchor=W)
        info = []
        saveButton=Button(buttonFrame, text="SAVE", command=getInput)
        saveButton.pack(side=LEFT, anchor=W)
        nextButton=Button(buttonFrame, text="NEXT", command=lambda: controller.show_frame(EntryScreen))
        nextButton.pack(side=LEFT, anchor=W)
class EntryScreen(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        EntryScreen.configure(self, bg="light blue")
        mainFrame = Frame(self)
        mainFrame.pack(anchor=NW)
        spinFrame= (mainFrame)
        spinFrame.pack(side=LEFT, anchor=W)
        chooseFrame=(mainFrame)
        chooseFrame.pack(side=LEFT, anchor=W)
        buttonFrame=Frame(mainFrame)
        buttonFrame.pack(side=LEFT, anchor=SW)
        button1Frame=Frame(mainFrame)
        button1Frame.pack(side=BOTTOM, anchor=SW)
        spinLabel = Label(spinFrame, text="Choose Month", bg="light blue")
        spinLabel.pack(anchor=W)
        spin=Spinbox(chooseFrame, values=('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'))
        spin.pack(anchor=W)
        diseaseLabel = Label(spinFrame, text="Choose a Disease", bg="light blue")
        diseaseLabel.pack(anchor=W)
        diseaseSpin=Spinbox(chooseFrame, values=('Flu', 'malaria'))
        diseaseSpin.pack(anchor=W)
        textLabel = Label(spinFrame, text="Enter the amount", bg="light blue")
        textLabel.pack(anchor=W)
        textBox = Entry(chooseFrame)
        textBox.pack(anchor=W)
        def press():
            month = spin.get()
            amount = int(textBox.get())
            amount_list.append(amount)
            month_list.append(month)
            print(month_list, amount_list)
            textBox.delete(0, END)
        AddButton = Button(buttonFrame, text="ADD", command=press)
        AddButton.pack(side=LEFT, anchor=W)
        DisplayButton = Button(buttonFrame, text="DISPLAY")
        DisplayButton.pack(side=LEFT, anchor=W)
        ResetButton = Button(buttonFrame, text="RESET")
        ResetButton.pack(side=LEFT, anchor=W)
        graphButton = Button(button1Frame, text="GRAPH", command=lambda: controller.show_frame(GraphScreen))
        graphButton.pack(side=LEFT, anchor=W)
        backButton=Button(button1Frame, text="BACK", command=lambda: controller.show_frame(HomeScreen))
        backButton.pack(side=LEFT, anchor=W)
        homeButton=Button(button1Frame, text="HOME", command=lambda: controller.show_frame(HomeScreen))
        homeButton.pack(side=LEFT, anchor=W)
class GraphScreen(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        GraphScreen.configure(self, bg="light blue")
        months = np.array(month_list)
        amount = np.array(amount_list)
        fig = Figure(figsize=(6, 6), dpi=100)
        fig.patch.set_facecolor('lightblue')
        a = fig.add_subplot(1,1,1) #axisbg='light blue')
        a.scatter(months, amount, color='red')
        a.set_title ("Monthly Entry", fontsize=16)
        a.set_ylabel("number of patients", fontsize=12)
        a.set_xlabel("months", fontsize=14)
        a.set_facecolor('blue')
        canvas = FigureCanvasTkAgg(fig, self)
        canvas.get_tk_widget().pack(side=BOTTOM, fill=BOTH, expand=True)
        canvas.draw()
        backButton = Button(self, text="BACK", command=lambda: controller.show_frame(EntryScreen))
        backButton.pack(side=LEFT)
        mapButton=Button(self, text="MAP", command=lambda: controller.show_frame(MapScreen))
        mapButton.pack(side=LEFT)
        homeButton=Button(self, text="HOME", command=lambda: controller.show_frame(HomeScreen))
        homeButton.pack(side=LEFT)
class MapScreen(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        MapScreen.configure(self, bg="light blue")
        backButton = Button(self, text="Reset Data", fg="green", font="bold", bg="yellow", command=lambda: controller.show_frame(GraphScreen))
        backButton.pack()
        homeButton=Button(self, text="HOME", fg="green", font="bold", bg="yellow", command=lambda: controller.show_frame(HomeScreen))
        homeButton.pack()
app=wholescreen()
app.mainloop()

0 个答案:

没有答案