Search code examples
pythontkintercustomtkintertkmessagebox

CustomTkinterMessagebox error - bad screen distance "100.0"


I have made a tkinter GUI and used CTKMessagebox for pop-ups. This worked witouth problems, but somehow it stopped working and shows me the following error:

The line of code:

CTkMessagebox(master=app, title="Error", message="The number of simulations is not set!", icon="warning")
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.10/tkinter/__init__.py", line 1921, in __call__
    return self.func(*args)
  File "/home/pira/.local/lib/python3.10/site-packages/customtkinter/windows/widgets/ctk_button.py", line 554, in _clicked
    self._command()
  File "/home/pira/Documents/NeDeS/simulator/simulator.py", line 716, in start_event
    if self.check_edge_cases() == True:
  File "/home/pira/Documents/NeDeS/simulator/simulator.py", line 831, in check_edge_cases
    CTkMessagebox(master=app, title="Error", message="The number of simulations is not set!", icon="warning")
  File "/home/pira/.local/lib/python3.10/site-packages/CTkMessagebox/ctkmessagebox.py", line 230, in __init__
    self.info = customtkinter.CTkButton(self.frame_top,  width=1, height=self.height/2, corner_radius=0, text=self.message, font=self.font,
  File "/home/pira/.local/lib/python3.10/site-packages/customtkinter/windows/widgets/ctk_button.py", line 95, in __init__
    self._canvas = CTkCanvas(master=self,
  File "/home/pira/.local/lib/python3.10/site-packages/customtkinter/windows/widgets/core_rendering/ctk_canvas.py", line 31, in __init__
    super().__init__(*args, **kwargs)
  File "/usr/lib/python3.10/tkinter/__init__.py", line 2716, in __init__
    Widget.__init__(self, master, 'canvas', cnf, kw)
  File "/usr/lib/python3.10/tkinter/__init__.py", line 2601, in __init__
    self.tk.call((widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: bad screen distance "100.0"

I have tried increasing and decreasing the width manually, but that did not work. I have tried updating CTKMessagebox, but did not work.

However I found out that this certain piece of code causes the error:

    def draw_pre_attack_network(self):
        G = nx.DiGraph(self.adjacency_matrix)
        pos = nx.spring_layout(G, seed=3113794652)  # positions for all nodes

        plt.figure()

        # Create the legend.
        legend_elements = [
            Line2D([0], [0], marker='o', color='w', label='Network',markerfacecolor='tab:orange', markersize=10),
            Line2D([0], [0], marker='o', color='w', label='Internet',markerfacecolor='maroon', markersize=10)
        ]
        plt.legend(handles=legend_elements, loc='best')

        # Draw the normal and hardened hosts.
        nx.draw(G, pos, node_color="tab:orange")


        # Draw the internet.
        _, internet = self.get_all_compromised_hosts()
        nx.draw(G, pos, nodelist=internet, node_color="maroon")

        labels = {}
        for n in G.nodes:
            add1, add2 = self.hosts[n].get_address()
            labels[n] = str(add1) + ", " + str(add2)

        nx.draw_networkx_labels(G, pos, labels, font_size=6, font_color="whitesmoke")
        # plt.show()
        plt.savefig(f"./created_network.png", format="PNG")
        plt.close()

Solution

  • Try working through your code and making sure that all the pixel dimensions that you supply (eg height, width etc) are integers.

    For example look at the line (referenced in the error message):

    self.info = customtkinter.CTkButton(self.frame_top,  width=1, height=self.height/2, corner_radius=0, text=self.message, font=self.font . . . 
    

    You have set the height to self.height/2. This will be a float regardless of whether self.height divides by 2 or not (and CTkButton is expecting an integer). To fix this particuar instance you could use:

    height=int(self.height/2)
    

    And there may be other similar occasions in your code where you are supplying a float where an int is expected.