首页 > 解决方案 > 在 Python 中使用 tkinter 的全屏帧

问题描述

我在程序中有几帧。但我希望它们都是全屏的。我试过 self.attribute('fullscreen', True) 和 self.overrideredirect(True)。我还计算了屏幕分辨率并试图以这种方式计算出来。但它们都不起作用。

无论使用哪种显示器,我想要的只是让所有帧都全屏显示。这是我的代码

谢谢

from tkinter import *

import tkinter as tk




LARGE_FONT= ("Verdana", 12)




class CMS(tk.Tk):



 
    def __init__(self, *args, **kwargs):
 

        tk.Tk.__init__(self, *args, **kwargs)




    #self.attributes('zoomen', True)      This line will cause a complier error
    #self.overrideredirect(True)          This line does not so anything. No full screen
    self.attributes('-topmost', True)
    #self.attributes('-fullscreen', True) This line creates a blank full screen with the StartPage screen on top of it.
                                             # The StartPage is not full screen.
  


        # Create Windows, Frames

        container = tk.Frame(self)


        container.pack(side="top", fill="both", expand = True)



         # frame size

        container.grid_rowconfigure(0, minsize = 500)

        container.grid_columnconfigure(0, minsize = 800)


 
        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)


    
          global home_btn

              global lights_btn



              #images of the buttons

              home_btn = PhotoImage(file='/home/pi/Documents/Bobby/Pictures/Home_Off.png')
      
        lights_btn = PhotoImage(file='/home/pi/Documents/Bobby/Pictures/Lighting_Off.png')


              label = tk.Label(self, text="Main Menu", font=LARGE_FONT)

              self.configure(bg='black') # background color

              label.pack(pady=10,padx=10)


              button = tk.Button(self, text="Visit Page 1",

                                 command=lambda: controller.show_frame(PageOne))

              button.pack()



              button2 = tk.Button(self, text="Visit Page 2",

                                  command=lambda: controller.show_frame(PageTwo))

              button2.pack()


        
      home = tk.Button(self, image=home_btn,
    
                           command=lambda: controller.show_frame(PageOne))


              home.pack()



              lights = tk.Button(self, image=lights_btn,
      
                           command=lambda: controller.show_frame(PageOne))

              lights.pack()


 

class PageOne(tk.Frame):



      def __init__(self, parent, controller):


              tk.Frame.__init__(self, parent)

              label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)

              label.pack(pady=10,padx=10)



              button1 = tk.Button(self, text="Back to Home",

                            command=lambda: controller.show_frame(StartPage))


              button1.pack()



        button2 = tk.Button(self, text="Page Two",

                            command=lambda: controller.show_frame(PageTwo))

        button2.pack()




class PageTwo(tk.Frame):


       def __init__(self, parent, controller):


              tk.Frame.__init__(self, parent)

              label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT)

              label.pack(pady=10,padx=10)



              button1 = tk.Button(self, text="Back to Home",

                            command=lambda: controller.show_frame(StartPage))

              button1.pack()



              button2 = tk.Button(self, text="Page One",

                            command=lambda: controller.show_frame(PageOne))

              button2.pack()




app = CMS()
app.mainloop()




app = CMS()
app.mainloop()

标签: pythontkinterraspberry-pi4

解决方案


推荐阅读