Skip to content

Instantly share code, notes, and snippets.

@JHay0112
Last active August 31, 2020 23:51
Show Gist options
  • Save JHay0112/a31b912300db4d0c92f66047f38a4800 to your computer and use it in GitHub Desktop.
Save JHay0112/a31b912300db4d0c92f66047f38a4800 to your computer and use it in GitHub Desktop.
Python Tkinter GUI Inventory Management
'''
inventorymanagment.py
Author: Jordan Hay
Date: 2020-08-20
'''
# -- Imports --
import tkinter as tk
from tkinter import ttk
from copy import copy
# -- Classes --
# An item object, tend to be apart of inventories
class Item:
# initialisation
def __init__(self, name, health_buff = 0):
self._name = name # Set name
self._health_buff = health_buff
def name(self):
return(self._name) # Return name
def health_buff(self):
return(self._health_buff)
# An inventory, holds items
class Inventory:
# initialisation
def __init__(self, parent = None, control = True):
self._items = []
self._parent = parent
self._control = control
if(parent != None):
self._frame = tk.Frame(parent, width = 100) # GUI Frame
self._frame.pack(fill = tk.BOTH)
# Position in menu
self._position = 0
# Holds item info being displate
self._item_frame = tk.Frame(self._frame, height = 40)
self._item_frame.pack_propagate(False)
self._item_frame.pack(fill = tk.X, padx = 5, pady = 5)
# Holds control buttons
self._control_frame = tk.Frame(self._frame)
self._control_frame.pack(fill = tk.X)
# Navigate up through inventory
self._up = ttk.Button(self._control_frame, text = "↑", state = tk.DISABLED, command = self.up)
self._up.pack(side = tk.LEFT, expand = True)
# Navigate down through inventory
self._down = ttk.Button(self._control_frame, text = "↓", state = tk.DISABLED, command = self.down)
self._down.pack(side = tk.LEFT, expand = True)
# Use item if it has any buffs or equipability
self._use = ttk.Button(self._control_frame, text = "USE", state = tk.DISABLED)
self._use.pack(side = tk.LEFT, expand = True)
# Discard the item
self._drop = ttk.Button(self._control_frame, text = "DROP", state = tk.DISABLED)
self._drop.pack(side = tk.LEFT, expand = True)
# Item transfer frame
self._transfer_frame = tk.Frame(self._parent)
self._transfer_frame.pack(fill = tk.BOTH)
# Refresh items
self.refresh()
def refresh(self):
if(self._parent == None):
return
# Clear all widgets in the item frame
for widget in self._item_frame.winfo_children():
widget.destroy()
# Check if items is not empty
if(self._items != []):
# Get current item
try:
# Try get current item
item = self._items[self._position]
except:
# If that doesn't work then try and move position up
self.up()
self.refresh()
return
# Enable item discard
self._drop.config(state = tk.ACTIVE)
self._drop.config(command = lambda: self.drop_item(item))
# Check up button activity
if(self._position == 0):
self._up.config(state = tk.DISABLED)
else:
self._up.config(state = tk.ACTIVE)
# Check down button activity
if(self._position >= len(self._items) - 1):
self._down.config(state = tk.DISABLED)
else:
self._down.config(state = tk.ACTIVE)
# Name of item
tk.Label(self._item_frame, text = item.name(), anchor = tk.W).pack(fill = tk.X)
# Check if item is useable
if(item.health_buff() != 0):
tk.Label(self._item_frame, text = f"Health Effect: {item.health_buff()}", anchor = tk.W).pack(fill = tk.X)
self._use.config(state = tk.ACTIVE)
self._use.config(command = lambda: self.consume_item(item))
else:
self._use.config(state = tk.DISABLED)
else:
# Disable all buttons
self._drop.config(state = tk.DISABLED)
self._up.config(state = tk.DISABLED)
self._down.config(state = tk.DISABLED)
self._use.config(state = tk.DISABLED)
# Inventory is empty
tk.Label(self._item_frame, text = "This inventory is empty!").pack(pady = 10)
# If there is no controls
if(not self._control):
# Disable drop and use
self._drop.config(state = tk.DISABLED)
self._use.config(state = tk.DISABLED)
def items(self):
return(self._items)
def current_item(self):
return(self._items[self._position])
def current_item_position(self):
return(self._position)
# Add an item to the inventory
def add_item(self, item):
self._items.append(item) # Append to the item list
self.refresh()
# Add multiple items to the inventory
def add_items(self, items):
# For every item in the list
for item in items:
self.add_item(item) # Add item
# Discard the item
def drop_item(self, item):
self._items.remove(item)
self.refresh()
# Consume item
def consume_item(self, item):
self.drop_item(item)
# Navigate up
def up(self):
self._position -= 1
self.refresh()
# Navigate up
def down(self):
self._position += 1
self.refresh()
# Allow the user to transfer items from one inventory to another
def transfer_mode(self, oinvent):
# Horizontal line
tk.Frame(self._transfer_frame, bg = "grey").pack(fill = tk.BOTH, pady = 5, padx = 3)
# Frame for transfer buttons
transfer_control_frame = tk.Frame(self._transfer_frame, width = 100, height = 25)
transfer_control_frame.pack(fill = tk.BOTH)
# Horizontal line
tk.Frame(self._transfer_frame, bg = "grey").pack(fill = tk.BOTH, pady = 5, padx = 3)
# Frame for other inventory
oinvent_items = oinvent.items()
oinvent = Inventory(self._transfer_frame, False)
oinvent.add_items(oinvent_items)
# Transfer label
tk.Label(transfer_control_frame, text = "Transfer: ").pack(side = tk.LEFT)
# Move something "up" from other invent into own invent
self._transfer_up = ttk.Button(transfer_control_frame, text = "↑", state = tk.DISABLED,
command = lambda inv = oinvent : self.transfer_from(inv))
self._transfer_up.pack(fill = tk.BOTH, side = tk.LEFT, expand = True)
# Move something "down" from own invent to other invent
self._transfer_down = ttk.Button(transfer_control_frame, text = "↓", state = tk.DISABLED,
command = lambda inv = oinvent : self.transfer_to(inv))
self._transfer_down.pack(fill = tk.BOTH, side = tk.LEFT, expand = True)
# Button to exit transfer mode
ttk.Button(transfer_control_frame, text = "EXIT", command = self.transfer_exit).pack(fill = tk.BOTH, side = tk.LEFT, expand = True)
if(self.items() != []):
self._transfer_down.config(state = tk.ACTIVE)
if(oinvent.items() != []):
self._transfer_up.config(state = tk.ACTIVE)
# Refresh transfer buttons
def transfer_refresh(self, oinvent):
if(self.items() != []):
self._transfer_down.config(state = tk.ACTIVE)
else:
self._transfer_down.config(state = tk.DISABLED)
if(oinvent.items() != []):
self._transfer_up.config(state = tk.ACTIVE)
else:
self._transfer_up.config(state = tk.DISABLED)
# Transfer from our inventory
def transfer_from(self, oinvent):
item = oinvent.current_item()
self.add_item(item)
oinvent.drop_item(item)
self.transfer_refresh(oinvent)
# Transfer to our inventory
def transfer_to(self, oinvent):
item = self.current_item()
oinvent.add_item(item)
self.drop_item(item)
self.transfer_refresh(oinvent)
# Exit transfer window
def transfer_exit(self):
for widget in self._transfer_frame.winfo_children():
widget.destroy()
# -- Main --
root = tk.Tk() # Root GUI object
if __name__ == "__main__":
# TK setup
root.title("Inventory")
# create a new inventory
invent = Inventory(root)
new_invent = Inventory()
# Add an item to the inventory
invent.add_item(Item("Test"))
invent.add_item(Item("The 1919 British Empire", 50))
invent.add_item(Item("Full Scale Replica of David Bowie in Labyrinth", 700))
new_invent.add_item(Item("Key #1"))
invent.transfer_mode(new_invent)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment