0

I have a tkinter app program written and everything works without getting errors. I converted the tkinter .py to a .exe by using pyinstaller. This application should be able to access through remote computers (desktops or laptops). But the problem is when trying remote desktop, the app won't open.

I think it's because I used .place() to place the widgets and x , y values are not the same for each computer. But I am also having some plots which are clearing by a button press. For that, .get_tk_widget().place_forget() is much easier and works well. And I know that I cannot mix grid and place in the same program.

How can I make the program work with any computer after converting to a .exe? The following is a simplified example of a much longer code.

import tkinter as tk
from tkinter import ttk
from tkmacosx import Button
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg)
import numpy as np

plt.close("all")

class Window(tk.Tk):
    def __init__(self,*args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.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 (StartPage, PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent,bg='#192232') 
        toolbar0 = tk.Frame(self,bg="#393951")
        toolbar0.pack(side=tk.TOP, fill=tk.X)
        label = ttk.Label(toolbar0, text="OLE Analyser")
        label.pack(pady=7,padx=10)
        button = Button(toolbar0, text="Analysis",command=lambda: controller.show_frame(PageOne))
        button.place(x=1600,y=6)   

class PageOne(tk.Frame):
    def __init__(self,parent, controller):
        tk.Frame.__init__(self, parent,bg='#192232')
        toolbar1 = tk.Frame(self,bg="#393951")
        toolbar1.pack(side=tk.TOP, fill=tk.X)
        
        button1 = Button(toolbar1,text="Home",command=lambda: controller.show_frame(StartPage))
        button1.place(x=1600,y=6)
        button2 = Button(toolbar1,text="Comparison",command=lambda: controller.show_frame(PageTwo))
        button2.place(x=1700,y=6) 

        fig, ax = plt.subplots(figsize=(9.05,6.43))
        canvas1 = FigureCanvasTkAgg(fig,self) 
        
        x1 = np.arange(1,100,2)
        y1 = x1**2 
        ax.plot(x1,y1)
        canvas1.draw()
        canvas1.get_tk_widget().place(x=272,y=103)
        
        def clear_graph():
            canvas1.get_tk_widget().place_forget() 
            
        clear_btn = Button(self,text="Clear Graph",command=lambda:clear_graph())
        clear_btn.place(x=165,y=927)        
   
class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent,bg='#192232')
        toolbar2 = tk.Frame(self,bg="#393951")
        toolbar2.pack(side=tk.TOP, fill=tk.X)
 
if __name__ == "__main__":
    app = Window()             
    ww = 1920
    hw = 1009
                
    w = app.winfo_screenwidth()
    h = app.winfo_screenheight() 
        
    x = (w/2) - (ww/2)-8
    y = (h/2) - (hw/2)-36
            
    app.geometry('%dx%d+%d+%d' % (ww, hw, x, y))
    app.update()
    app.mainloop()```
Chat0924
  • 41
  • 7
  • 4
    "And I know that I cannot mix grid and place in the same program." ~ Yes you can. You cannot mix `grid` and `pack` in the same **widget**. ie. If you use `pack` in a `frame` you cannot also use `grid` in that same `frame`. You can use `place` all you like, regardless. – OneMadGypsy Feb 22 '23 at 16:04
  • If I use `grid`, can I still use custom places for different `x`s and `y`s? And will it resolve the problem of not working the code in different computers? – Chat0924 Feb 22 '23 at 16:16
  • `grid` is used to align widgets with a row/column approach. `pack` is used to "dock" widgets to an available edge, even if that edge is created by another widget. `place` is used to put widgets at specific x/y coordinates. I do not understand your question, and I do not know if it will solve your issue. – OneMadGypsy Feb 22 '23 at 16:35

1 Answers1

2

My best answer to this question is that you set app.geometry to a specific number, such as;

app.geometry("800x600")

Using this, you can possibly alter your x and y values to bring them into such a frame, allowing for superfluous usage across resolutions. Also, depending on your use case, using

grid()

may be more appropriate than your current button implementation method, as it creates less room for your x & y values to be terribly off in a program that doesn't necessarily rely on x & y precise placement.