我剛剛開始在python中編程,我正在使用它來製作一個帶有GUI(使用tkinter)的小程序,它可以從一個excel文件(使用openpyxl)。讓用戶在需要時改變它們,然後使用按鈕將數據從條目寫入數據文件(.dat)(最好通過更新defaultdict數組中的值然後編寫.dat文件),並使用建模語言(pyomo)用所提供的數據創建一個模型並用cbc解算器解決它。Python> tkinter:如何從筆記本框架內的面板獲取值
我現在是沙發,我做了模型,圖形界面(這是從Excel文件中填充數據)。但是我無法獲取條目字段中的數據(要寫入更新defaultdicts數組)。我理解stackoverflow上的簡單示例(使用entry.get()),但它不適用於我的示例(可能是因爲我使用notbook選項卡,面板和框架,或者我搞砸了一些東西)。
我使用筆記本標籤而不是一個頁面,因爲我會在整個程序中包含更多(約5個)其他類別的數據。最後,我想讓程序能夠適應輸入(所以它不知道是否會有3,8或10個設施)。我使用python版本3.5.1。這裏是鏈接到Excel文件:https://drive.google.com/file/d/0B5vmtJnltudJWW4xakZlYnQ3RTg/view?usp=sharing
import sys
from tkinter import ttk
import tkinter as tk
import openpyxl
import numpy as np
import os
from collections import defaultdict
from facility_panel import *
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.getdata()
self.tabes()
button_box = tk.Frame(self)
tk.Button(button_box, text='Create Planning', command=self.on_ok_clicked).grid(pady=15)
button_box.pack()
self.create_menu()
self.set_keybindings()
@staticmethod
def center_on_screen(toplevel):
toplevel.update_idletasks()
w = toplevel.winfo_screenwidth()
h = toplevel.winfo_screenheight()
size = tuple(int(_) for _ in toplevel.geometry().split('+')[0].split('x'))
x = w/2 - size[0]/2
y = h/2 - size[1]/2
toplevel.geometry('%dx%d+%d+%d' % (size + (x, y)))
def set_keybindings(self):
self.bind_all('<Control-o>', lambda event: self.open_file())
self.bind_all('<Control-s>', lambda event: self.save_file())
self.bind_all('<Control-q>', self.quit_app)
self.bind_all('<Control-h>', lambda event: self.show_help())
self.bind_all('<Return>', lambda event: self.on_ok_clicked())
def on_ok_clicked(self):
print ('Entry text: %s' % self.entry.get())
print ('Scale value: %.1f' % self.scale.get())
print ('Checkbutton value: %i' % self.checkbox_val.get())
print ('Spinbox value: %i' % int(self.spinbox.get()))
print ('OptionMenu value: %s' % self.enum_val.get())
def create_menu(self):
menubar = tk.Menu(self)
fileMenu = tk.Menu(menubar, tearoff=False)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
fileMenu.add_command(label="Open", underline=1, command=self.open_file, accelerator="Ctrl+O")
fileMenu.add_command(label="Save", underline=1, command=self.save_file, accelerator="Ctrl+S")
fileMenu.add_command(label="Quit", underline=1, command=self.quit_app, accelerator="Ctrl+Q")
helpMenu = tk.Menu(menubar, tearoff=False)
menubar.add_cascade(label="Help", underline=0, menu=helpMenu)
helpMenu.add_command(label="Help", underline=1, command=self.show_help, accelerator="Ctrl+H")
helpMenu.add_command(label="About", underline=1, command=self.about_app)
self.config(menu=menubar)
def open_file(self):
"""Options are explained here: http://tkinter.unpythonic.net/wiki/tkFileDialog"""
filename = askopenfilename(title='Open a file')
if filename:
print ('Open and do something with %s' % filename)
def save_file(self):
"""Options are explained here: http://tkinter.unpythonic.net/wiki/tkFileDialog"""
filename = asksaveasfilename()
if filename:
print ('Save something to %s' % filename)
def quit_app(self):
app.destroy()
def show_help(self):
# FIXME: pressing return correctly closes dialog, but also incorrectly fires the main window's 'on_click' method
about_text = """
Contact: \n
[email protected]"""
about_dialog = tk.Toplevel(self)
about_dialog.title('About App')
about_dialog.bind('<Escape>', lambda event: about_dialog.destroy())
about_dialog.bind('<Return>', lambda event: about_dialog.destroy())
App.center_on_screen(about_dialog)
tk.Message(about_dialog, text=about_text).pack()
button = tk.Button(about_dialog, text='Close', command=about_dialog.destroy).pack()
def about_app(self):
# FIXME: pressing return correctly closes dialog, but also incorrectly fires the main window's 'on_click' method
about_text = """
This application is made by Jan Jansen\n
version 0.7"""
about_dialog = tk.Toplevel(self)
about_dialog.title('About App')
about_dialog.bind('<Escape>', lambda event: about_dialog.destroy())
about_dialog.bind('<Return>', lambda event: about_dialog.destroy())
App.center_on_screen(about_dialog)
tk.Message(about_dialog, text=about_text).pack()
button = tk.Button(about_dialog, text='Close', command=about_dialog.destroy).pack()
def tabes(self):
nb = ttk.Notebook()
nb.pack(expand=1, fill="both")
# Frame to hold contentx
frame = tk.Frame(nb)
vscroll = tk.Scrollbar(frame, orient="vertical")
#panel['yscroll'] = vscroll.set
vscroll.pack(side="right", fill="y")
for facilityname in Facilities:
panel = FacilityPanel(frame, facilityname, capfacility[facilityname], safetystock[facilityname], maxpressure[facilityname], str(compulsorystarttime[facilityname]), str(compulsoryendtime[facilityname]), demandmatrix[facilityname][1], demandmatrix[facilityname][2], demandmatrix[facilityname][3], demandmatrix[facilityname][4], demandmatrix[facilityname][5], demandmatrix[facilityname][6], demandmatrix[facilityname][7])
panel.pack(fill="both")
# add to notebook (underline = index for short-cut character)
nb.add(frame, text='Facilities', underline=0, padding=2)
#--------------------------------------------------------------------------------------------------------
def getdata(self):
wb = openpyxl.load_workbook("data.xlsx")
ws = wb["Facilities"]
global Facilities
Facilities = ([])
row_count = ws.max_row
column_count = ws.max_column
global initlevel
initlevel = defaultdict(dict)
global capfacility
capfacility = defaultdict(dict)
global safetystock
safetystock = defaultdict(dict)
global maxpressure
maxpressure = defaultdict(dict)
global pressureincrease
pressureincrease = defaultdict(dict)
global compulsorystarttime
compulsorystarttime = defaultdict(dict)
global compulsoryendtime
compulsoryendtime = defaultdict(dict)
global demandmatrix
demandmatrix = defaultdict(dict)
for i in range(3, row_count+1, 1):
Facilities.append(ws.cell(row = i, column = 1).value)
initlevel[ws.cell(row = i, column = 1).value] = ws.cell(row = i, column = 2).value
capfacility[ws.cell(row = i, column = 1).value] = ws.cell(row = i, column = 3).value
safetystock[ws.cell(row = i, column = 1).value] = ws.cell(row = i, column = 4).value
maxpressure[ws.cell(row = i, column = 1).value] = ws.cell(row = i, column = 5).value
pressureincrease[ws.cell(row = i, column = 1).value] = ws.cell(row = i, column = 6).value
compulsorystarttime[ws.cell(row = i, column = 1).value] = ws.cell(row = i, column = 7).value
compulsoryendtime[ws.cell(row = i, column = 1).value] = ws.cell(row = i, column = 8).value
for j in range (9, column_count+1, 1):
demandmatrix[ws.cell(row = i, column = 1).value][ws.cell(row = 2, column = j).value] = ws.cell(row = i, column = j).value
if __name__ == "__main__":
app = App()
app.title("Planning")
toplevel = app.winfo_toplevel()
toplevel.wm_state('zoomed')
app.mainloop()
這是我做了類: 從Tkinter的進口*
class FacilityPanel(Frame):
def __init__(self, app, facility_name, capacity, safetystock, maxpressure, compulsorystarttime, compulsoryendtime, demandma, demanddi, demandwo, demanddo, demandvr, demandza, demandzo):
Frame.__init__(self, app)
group = LabelFrame(self, text=facility_name)
group.pack(side =LEFT)
group.enable_facility = IntVar()
enable_button = Checkbutton(group, variable = group.enable_facility,
command = print("toggle"))
enable_button.pack(side = LEFT)
enable_button.select()
group.init_level = IntVar()
init_label = Label(group, text="Current Level: 5,06 m\u00B3")
init_label.pack(side = LEFT)
group.capacity = DoubleVar()
capacity_label = Label(group, text="Capacity:")
capacity_label.pack(side = LEFT)
capacity_entry = Entry(group, width=8)
capacity_entry.pack(side = LEFT)
capacity_entry.insert(0, capacity)
capacity_label_unit = Label(group, text="kg ")
capacity_label_unit.pack(side = LEFT)
group.safetystock = DoubleVar()
safetystock_label = Label(group, text="Safetystock:")
safetystock_label.pack(side = LEFT)
safetystock_entry = Entry(group, width=8)
safetystock_entry.pack(side = LEFT)
safetystock_entry.insert(0, safetystock)
safetystock_label_unit = Label(group, text="kg ")
safetystock_label_unit.pack(side = LEFT)
group.maxpressure = DoubleVar()
maxpressure_label = Label(group, text="Maxpressure:")
maxpressure_label.pack(side = LEFT)
maxpressure_entry = Entry(group, width=8)
maxpressure_entry.pack(side = LEFT)
maxpressure_entry.insert(0, maxpressure)
maxpressure_label_unit = Label(group, text="bar ")
maxpressure_label_unit.pack(side = LEFT)
group.comp_start_time = DoubleVar()
comp_time1_label = Label(group, text="Unload time window:")
comp_time1_label.pack(side = LEFT)
comp_start_time_entry = Entry(group, width=8)
comp_start_time_entry.pack(side = LEFT)
comp_start_time_entry.insert(0, compulsorystarttime)
comp_time2_label = Label(group, text="-")
comp_time2_label.pack(side = LEFT)
comp_end_time_entry = Entry(group, width=8)
comp_end_time_entry.pack(side = LEFT)
comp_end_time_entry.insert(0, compulsoryendtime)
comp_time3_label = Label(group, text="hours ")
comp_time3_label.pack(side = LEFT)
group.demandmaandag = DoubleVar()
demandmaandag_label = Label(group, text="Maandag:")
demandmaandag_label.pack(side = LEFT)
demandmaandag_entry = Entry(group, width=8)
demandmaandag_entry.pack(side = LEFT)
demandmaandag_entry.insert(0, demandma)
demandmaandag_label_unit = Label(group, text="kg ")
demandmaandag_label_unit.pack(side = LEFT)
group.demanddinsdag = DoubleVar()
demanddinsdag_label = Label(group, text="Dinsdag:")
demanddinsdag_label.pack(side = LEFT)
demanddinsdag_entry = Entry(group, width=8)
demanddinsdag_entry.pack(side = LEFT)
demanddinsdag_entry.insert(0, demanddi)
demanddinsdag_label_unit = Label(group, text="kg ")
demanddinsdag_label_unit.pack(side = LEFT)
group.demandwoensdag = DoubleVar()
demandwoensdag_label = Label(group, text="Woensdag:")
demandwoensdag_label.pack(side = LEFT)
demandwoensdag_entry = Entry(group, width=8)
demandwoensdag_entry.pack(side = LEFT)
demandwoensdag_entry.insert(0, demandwo)
demandwoensdag_label_unit = Label(group, text="kg ")
demandwoensdag_label_unit.pack(side = LEFT)
group.demanddonderdag = DoubleVar()
demanddonderdag_label = Label(group, text="Donderdag:")
demanddonderdag_label.pack(side = LEFT)
demanddonderdag_entry = Entry(group, width=8)
demanddonderdag_entry.pack(side = LEFT)
demanddonderdag_entry.insert(0, demanddo)
demanddonderdag_label_unit = Label(group, text="kg ")
demanddonderdag_label_unit.pack(side = LEFT)
group.demandvrijdag = DoubleVar()
demandvrijdag_label = Label(group, text="Vrijdag:")
demandvrijdag_label.pack(side = LEFT)
demandvrijdag_entry = Entry(group, width=8)
demandvrijdag_entry.pack(side = LEFT)
demandvrijdag_entry.insert(0, demandvr)
demandvrijdag_label_unit = Label(group, text="kg ")
demandvrijdag_label_unit.pack(side = LEFT)
group.demandzaterdag = DoubleVar()
demandzaterdag_label = Label(group, text="Zaterdag:")
demandzaterdag_label.pack(side = LEFT)
demandzaterdag_entry = Entry(group, width=8)
demandzaterdag_entry.pack(side = LEFT)
demandzaterdag_entry.insert(0, demandza)
demandzaterdag_label_unit = Label(group, text="kg ")
demandzaterdag_label_unit.pack(side = LEFT)
group.demandzaterdag = DoubleVar()
demandzondag_label = Label(group, text="Zondag:")
demandzondag_label.pack(side = LEFT)
demandzondag_entry = Entry(group, width=8)
demandzondag_entry.pack(side = LEFT)
demandzondag_entry.insert(0, demandzo)
demandzondag_label_unit = Label(group, text="kg ")
demandzondag_label_unit.pack(side = LEFT)
TL; DR。信息和代碼太多。請編輯您的帖子以包含展示您的問題所需的*最低*信息和代碼。 Openpyxl和pyomo以及cbc求解器似乎與手邊的問題沒有關係,所以不必提及。 – SiHa