【问题标题】:Tkinter Label Won't fit in the window after creating an executable with pyinstaller使用 pyinstaller 创建可执行文件后,Tkinter 标签不适合窗口
【发布时间】:2020-08-09 12:32:55
【问题描述】:

我使用 pyinstaller 创建了一个应用程序,但在某些将使用它的机器上,从窗口中删除了一些标签,另外还有两个文本框,see the image。我该如何解决这个问题? 在我的机器上它适合,在另一个不适合。 我认为问题在于 place() 方法,但我不知道为什么它在计算机中有效而在另一台计算机中无效。

import tkinter as tk
from tkinter import ttk
import tkinter.font as font
import tkinter.messagebox

class Application(ttk.Frame):   
    def __init__(self, main_window):
        super().__init__(main_window)
        self.place(relwidth=1, relheight=1)
        ttk.Style(main_window)
        font.nametofont("TkTextFont").configure(size=12)
        font.nametofont("TkDefaultFont").configure(size=12)
        #Cabecalho
        ttk.Label(self, text="Requisições", font='arial 18 bold').place(x=150, y=5)
        ttk.Label(self, text="Desligamento", font='arial  18 bold').place(x=142, y=35)
        #Informações usúario
        ttk.Label(self, text="Usuário", font='arial  16 bold').place(x=10, y=100)
        ttk.Label(self, text="Email:", font='arial  14').place(x=10, y=128)
        ttk.Entry(self, width=20).place(x=85, y=128)
        ttk.Label(self, text="Senha:", font='arial  14').place(x=10, y=158)
        ttk.Entry(self,  show="•").place(x=85, y=158)
        #Lote
        ttk.Label(self, text="Lote", font='arial 16 bold').place(x=10, y=190)
        ttk.Label(self, text="Início:", font='arial  14').place(x=10, y=220)
        ttk.Entry(self, width=10).place(x=85, y=220)
        ttk.Label(self, text="Final:", font='arial  14').place(x=10, y=248)
        ttk.Entry(self, width=10).place(x=85, y=250)
        ttk.Label(self,text="dd/mm/aaaa",foreground='red',font='arial 10 italic').place(x=93, y=280)
        ttk.Button(self,text= 'Download').place(x=210, y=215)
        ttk.Button(self,text= 'Planilha').place(x=210, y=247)
        #Informações
        ttk.Label(self, text="Informações",font='arial 16 bold').place(x=10, y=310)
        ttk.Button(self, text="Anexado").place(x=160, y=305)
        ttk.Button(self, text="Pendente").place(x=280, y=305)
        #Montagem listbox
        frame = tk.Frame(main_window)
        frame.pack()
        frame.place(x=10,y=340)
        self.listbox = tk.Listbox(frame,width=45, height=8, selectbackground='#808080', selectmode= 'extended')
        self.listbox.pack(side="left", fill="y")
        self.scrollbar = ttk.Scrollbar(frame, orient="vertical")
        self.scrollbar.config(command=self.listbox.yview)
        self.scrollbar.pack(side="right", fill="y")
        self.listbox.config(yscrollcommand=self.scrollbar.set)
        ttk.Button(self, text="Exibir").place(x=10, y=525)
        ttk.Button(self, text="Aprovada").place(x=160, y=525)
        ttk.Button(self, text="E mail Ponto").place(x=310, y=525)
        ttk.Button(self, text="E mail Pendente", ).place(x=10, y=560)
        # Informações da carta a ser exibida
        ttk.Label(self,text="Requisição:"      , font='arial 14 bold').place(x=450 , y=10)       
        ttk.Label(self,text="Nome:"            , font='arial 14 bold').place(x=450 , y=40)             
        ttk.Label(self,text="Aviso Prévio:"    , font='arial 14 bold').place(x=1000, y=40)        
        ttk.Label(self,text="Motivo:"          , font='arial 14 bold').place(x=650 , y=10)           
        ttk.Label(self,text="Comunicação:"     , font='arial 14 bold').place(x=1010, y=10)     
        ttk.Label(self,text="Desligamento:"    , font='arial 14 bold').place(x=1300, y=10)    
        ttk.Label(self,text="Matrícula:"       , font='arial 14 bold').place(x=1310, y=40)       
        ttk.Label(self,text="Salário:"         , font='arial 14 bold').place(x=450 , y=70)          
        ttk.Label(self,text="Admissão:"        , font='arial 14 bold').place(x=650 , y=70)         
        ttk.Label(self,text="Atualização:"     , font='arial 14 bold').place(x=910, y=70)
        ttk.Label(self,text="Cargo:"           , font='arial 14 bold').place(x=1160, y=70)
        #Montagem Janela Observação
        ttk.Label(self,text="Observações", font='arial 14 bold').place(x=1210, y=100)
        self.obs = tk.Text(self,height=10, width=42, fg="black", bg="white", font=("arial ", 12))
        self.obs.place(x=1210, y=130)
        #Montagem caixa de texto
        ttk.Label(self, text="Carta Inconsistente", font='arial  14 bold').place(x=10, y=600)
        self.texto = tk.Text(self, height=9, width=45, fg="#0911D0", bg="white", font=("arial ", 12), cursor="pencil")
        self.texto.place(x=10, y=630)
        # Botão Enviar email para carta náo ok
        ttk.Button(self, text="Enviar E mail").place(x=10, y=800)
        # Botão Sair
       ttk.Button(self, text="Sair", command=self.quit).place(x=310, y=800)
def main():
    main_window = tk.Tk()
    main_window.title("Desligamentos")
    main_window.state("zoomed")
    app = Application(main_window)
    app.mainloop()  
if __name__=="__main__":
        main()

【问题讨论】:

  • 嗨 Lucas,你的代码在哪里?
  • 我敢打赌,您要么使用place,要么将窗口强制设置为特定大小。 Tkinter 擅长布置窗口以使一切都适合(如果可能的话),但前提是您要利用这样做的功能。
  • 您好 Fares,我是新来的,无法在此处发送代码。
  • Bryan,我在所有代码中都使用了 .place()。
  • @LucasPeixoto,您可以编辑问题并粘贴代码,但请确保使用正确的格式 (i.e. Ctrl + k , then paste)

标签: python tkinter textbox label pyinstaller


【解决方案1】:

您可以考虑这样的事情,固定 GUI 大小 top.resizable(0, 0),并使用 relxrelyrelheightrelwidthplace(),这些值是相对于父小部件的,所以如果您有嵌套的小部件(即标签框架内的文本框,则标签框架的值相对于顶部窗口,文本框的值相对于标签框架)。

try:
    from tkinter import *
except ImportError:
    from Tkinter import *

try:
    import tkinter.ttk as ttk
    py3 = 1
except ImportError:
    import ttk
    py3 = 0

def create_window():
    global root, top
    root = Tk()            # creating a tkinter window, Tk is a class
    top = MainFrame(root)  # building the gui, so it's like MainFrame is inheriting the Tk class
    root.mainloop()        # infinite main loop

class MainFrame:
    def __init__(self, top=None):
        self.style = ttk.Style()
        if sys.platform == "win32":
            self.style.theme_use('winnative')

        top.geometry("802x557+250+104")
        top.minsize(120, 1)
        top.maxsize(2970, 881)
        top.resizable(0, 0)      # fixing the GUI size

        self.Labelframe = LabelFrame(top)
        self.Labelframe.place(relx=0.4, rely=0.4, relheight=0.279, relwidth=0.264)
        self.Labelframe.configure(text='''Test label''', background="#d9d9d9")


if __name__ == '__main__':
    create_window()

【讨论】:

  • 关闭调整窗口大小的能力是惹恼用户的好方法,并且使这个问题变得更糟而不是更好,因为用户已经失去了使窗口更大以便看到剪切的能力关闭小部件。
  • @BryanOakley 我认为这取决于项目的要求,但这是一个很好的观点,可用性很重要。使用relx等应该足以解决我认为的问题,尝试在有和没有固定大小代码行的情况下运行代码。
  • 我会试试这个@FaresSalem。我正在考虑通过 self.pack(expand=True, fill='both') 更改 self.place(relwidth =1, relheight=1)。
  • @faressalem 我已经更改了 relx 和 trust 的 x 和 y,并且发生了其他失真。查看正在编辑的新图像。
  • @faressalem,现在可以了。问题是因为我在 1600x900 分辨率下仅使用 x 和 y 设置位置,在 1366x768 的机器上,小部件会切断窗口。现在我已经设置了 relx、rely、relwidth、relheight 和 anchor='center'
【解决方案2】:

只是一个建议,向.place() 添加一个名为anchor 的新参数。 Anchor 确定小部件的哪个部分到达指定的坐标。比如anchor = "center",relx和rely指定的坐标相对为0.5和0.5,那么widget的中间会去0.5、0.5。

有时锚点是 nw 或左上角,有时小部件会移到屏幕的一侧。

所以,将您的anchor 设置为“中心”,然后告诉我会发生什么。

希望这会有所帮助!

编辑:

这将是您的代码:

import tkinter as tk
from tkinter import ttk
import tkinter.font as font
import tkinter.messagebox

class Application(ttk.Frame):   
    def __init__(self, main_window):
        super().__init__(main_window)
        self.place(relwidth=1, relheight=1, anchor = "center")
        ttk.Style(main_window)
        font.nametofont("TkTextFont").configure(size=12)
        font.nametofont("TkDefaultFont").configure(size=12)
        #Cabecalho
        ttk.Label(self, text="Requisições", font='arial 18 bold').place(x=150, y=5, anchor = "center")
        ttk.Label(self, text="Desligamento", font='arial  18 bold').place(x=142, y=35, anchor = "center")
        #Informações usúario
        ttk.Label(self, text="Usuário", font='arial  16 bold').place(x=10, y=100, anchor = "center")
        ttk.Label(self, text="Email:", font='arial  14').place(x=10, y=128)
        ttk.Entry(self, width=20).place(x=85, y=128, anchor = "center")
        ttk.Label(self, text="Senha:", font='arial  14').place(x=10, y=158, anchor = "center")
        ttk.Entry(self,  show="•").place(x=85, y=158, anchor = "center")
        #Lote
        ttk.Label(self, text="Lote", font='arial 16 bold').place(x=10, y=190, anchor = "center")
        ttk.Label(self, text="Início:", font='arial  14').place(x=10, y=22, anchor = "center")
        ttk.Entry(self, width=10).place(x=85, y=220, anchor = "center")
        ttk.Label(self, text="Final:", font='arial  14').place(x=10, y=248, anchor = "center")
        ttk.Entry(self, width=10).place(x=85, y=250, anchor = "center")
        ttk.Label(self,text="dd/mm/aaaa",foreground='red',font='arial 10 italic').place(x=93, y=280, anchor = "center")
        ttk.Button(self,text= 'Download').place(x=210, y=215, anchor = "center")
        ttk.Button(self,text= 'Planilha').place(x=210, y=247, anchor = "center")
        #Informações
        ttk.Label(self, text="Informações",font='arial 16 bold').place(x=10, y=310, anchor = "center")
        ttk.Button(self, text="Anexado").place(x=160, y=305, anchor = "center")
        ttk.Button(self, text="Pendente").place(x=280, y=305, anchor = "center")
        #Montagem listbox
        frame = tk.Frame(main_window)
        frame.pack()
        frame.place(x=10,y=340, anchor = "center")
        self.listbox = tk.Listbox(frame,width=45, height=8, selectbackground='#808080', selectmode= 'extended')
        self.listbox.pack(side="left", fill="y")
        self.scrollbar = ttk.Scrollbar(frame, orient="vertical")
        self.scrollbar.config(command=self.listbox.yview)
        self.scrollbar.pack(side="right", fill="y")
        self.listbox.config(yscrollcommand=self.scrollbar.set)
        ttk.Button(self, text="Exibir").place(x=10, y=525, anchor = "center")
        ttk.Button(self, text="Aprovada").place(x=160, y=525, anchor = "center")
        ttk.Button(self, text="E mail Ponto").place(x=310, y=525, anchor = "center")
        ttk.Button(self, text="E mail Pendente", ).place(x=10, y=560, anchor = "center")
        # Informações da carta a ser exibida
        ttk.Label(self,text="Requisição:"      , font='arial 14 bold').place(x=450 , y=10, anchor = "center")       
        ttk.Label(self,text="Nome:"            , font='arial 14 bold').place(x=450 , y=40, anchor = "center")             
        ttk.Label(self,text="Aviso Prévio:"    , font='arial 14 bold').place(x=1000, y=40, anchor = "center")        
        ttk.Label(self,text="Motivo:"          , font='arial 14 bold').place(x=650 , y=10, anchor = "center")           
        ttk.Label(self,text="Comunicação:"     , font='arial 14 bold').place(x=1010, y=10, anchor = "center")     
        ttk.Label(self,text="Desligamento:"    , font='arial 14 bold').place(x=1300, y=10, anchor = "center")    
        ttk.Label(self,text="Matrícula:"       , font='arial 14 bold').place(x=1310, y=40, anchor = "center")       
        ttk.Label(self,text="Salário:"         , font='arial 14 bold').place(x=450 , y=70, anchor = "center")          
        ttk.Label(self,text="Admissão:"        , font='arial 14 bold').place(x=650 , y=70, anchor = "center")         
        ttk.Label(self,text="Atualização:"     , font='arial 14 bold').place(x=910, y=70, anchor = "center")
        ttk.Label(self,text="Cargo:"           , font='arial 14 bold').place(x=1160, y=70, anchor = "center")
        #Montagem Janela Observação
        ttk.Label(self,text="Observações", font='arial 14 bold').place(x=1210, y=100, anchor = "center")
        self.obs = tk.Text(self,height=10, width=42, fg="black", bg="white", font=("arial ", 12))
        self.obs.place(x=1210, y=130, anchor = "center")
        #Montagem caixa de texto
        ttk.Label(self, text="Carta Inconsistente", font='arial  14 bold').place(x=10, y=600, anchor = "center")
        self.texto = tk.Text(self, height=9, width=45, fg="#0911D0", bg="white", font=("arial ", 12), cursor="pencil")
        self.texto.place(x=10, y=630, anchor = "center")
        # Botão Enviar email para carta náo ok
        ttk.Button(self, text="Enviar E mail").place(x=10, y=800, anchor = "center")
        # Botão Sair
        ttk.Button(self, text="Sair", command=self.quit).place(x=310, y=800, anchor = "center")
def main():
    main_window = tk.Tk()
    main_window.title("Desligamentos")

    app = Application(main_window)
    app.mainloop()  
if __name__=="__main__":
        main()

请告诉我这是否有效!

【讨论】:

  • 另外,如果这不起作用,请记住 .place() 不是最好的几何管理器。我在显示器上运行了这个程序,一切都在屏幕上。但是,当我在计算机上正常运行它时,某些元素被切断了。这取决于您使用的屏幕尺寸。希望这能消除一些疑虑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-13
  • 2020-11-26
  • 1970-01-01
  • 2018-08-16
相关资源
最近更新 更多