To implement the changes that you've requested, I will provide you with the code for a new dialog class with the necessary Spinbox widgets, buttons, and functionality to handle grid division. Please note that this implementation assumes you're using Tkinter for your user interface. Adjustments might be necessary depending on the precise structure of your existing code and the GUI library you're using.
Here's how you might implement the GridOptionsDialog class and modify the toggle_grid method:
import tkinter as tk
from tkinter import simpledialog
class GridOptionsDialog(simpledialog.Dialog):
def __init__(self, parent, title=None):
self.div_x = tk.IntVar(value=3) # default division number for the x-axis
self.div_y = tk.IntVar(value=3) # default division number for the y-axis
super().__init__(parent, title)
def body(self, master):
tk.Label(master, text="Grid divisions (X-axis):").grid(row=0)
tk.Label(master, text="Grid divisions (Y-axis):").grid(row=1)
self.spin_x = tk.Spinbox(master, from_=1, to=10, textvariable=self.div_x)
self.spin_x.grid(row=0, column=1)
self.spin_y = tk.Spinbox(master, from_=1, to=10, textvariable=self.div_y)
self.spin_y.grid(row=1, column=1)
return self.spin_x # initial focus
def apply(self):
# No need to do anything here since values are directly updated via IntVars
pass
def disable_grid(self):
self.div_x.set(1)
self.div_y.set(1)
self.parent.grid_type = "a"
self.parent.display_image()
def create_buttons(self):
box = tk.Frame(self)
self.btn_disable = tk.Button(box, text="Disable Grid", width=10, command=self.disable_grid)
self.btn_disable.pack(side=tk.LEFT, padx=5, pady=5)
self.btn_apply = tk.Button(box, text="Apply", width=10, command=self.apply_and_refresh)
self.btn_apply.pack(side=tk.LEFT, padx=5, pady=5)
self.btn_ok = tk.Button(box, text="OK", width=10, command=self.ok)
self.btn_ok.pack(side=tk.LEFT, padx=5, pady=5)
self.bind("<Return>", self.ok)
box.pack()
def apply_and_refresh(self):
self.parent.grid_type = "b"
self.parent.div_x = self.div_x.get() # Fetches the value from the spinbox and store it
self.parent.div_y = self.div_y.get()
self.parent.display_image()
def ok(self, event=None):
self.apply_and_refresh()
super().ok()
class YourApp:
# ... Your existing code ...
# Assuming self.canvas exists as your drawing area and you have a method self.display_image()
def toggle_grid(self):
dialog = GridOptionsDialog(self, title="Grid Options")
# You can access the spinbox values using dialog.div_x.get() and dialog.div_y.get()
# after the dialog is closed, but for real-time updates, use callback methods
def display_image(self):
# ... Your existing display image method ...
pass
# Add your existing draw_grid implementation here, with modifications
# to use div_x and div_y for grid drawing if the grid_type is "b"
# Create the main application window and run the app
root = tk.Tk()
app = YourApp()
root.mainloop()
To integrate this into your existing application:
- Add the new
GridOptionsDialog class to handle the dialog window with spinboxes and buttons.
- Modify
toggle_grid in the YourApp class to open the dialog.
- Use the values from
dialog.div_x and dialog.div_y to determine grid divisions when drawing the grid.
- Ensure that
self.div_x and self.div_y are attributes of your YourApp class or obtained from the dialog and stored when needed.
If your application's code differs significantly from this outline, you may need to refactor it appropriately to achieve the desired functionality.