python代码如何做成应用程序_我正在创建一个应用程序作为一个项目。在下面的代码中,我如何在Python中使用pack?...

这是一个有多个屏幕的应用程序,我想在屏幕上按我的意愿放置小部件,我应该使用pack、grid或place的哪种方法?

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值