python Tkinter上的多窗口

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Tkinter上的多窗口相关的知识,希望对你有一定的参考价值。

from tkinter import *
import urllib.request
from tkinterhtml import HtmlFrame

# main content
class MainContent(Tk):

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

		self.title("Welcome to LikeGeeks app")

		# create conteainer
		container = 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 (HomeSection, Projects, Settings, AboutUs):
			frame = F(container, self)
			self.frames[F] = frame
			frame.grid(row=0, column=0, sticky="nsew")

		# show home
		self.show_frame(HomeSection)
		# init menu
		self.create_menu()

	# create menu
	def create_menu(self):
		menubar = Menu(self)
		# create a pulldown menu, and add it to the menu bar
		filemenu = Menu(menubar, tearoff=0)
		filemenu.add_command(label="Home", command=lambda: self.show_frame(HomeSection))
		filemenu.add_command(label="Projects", command=lambda: self.show_frame(Projects))
		filemenu.add_command(label="Settings", command=lambda: self.show_frame(Settings))
		filemenu.add_separator()
		filemenu.add_command(label="Exit", command=self.quit)
		menubar.add_cascade(label="Options", menu=filemenu)


		helpmenu = Menu(menubar, tearoff=0)
		helpmenu.add_command(label="About", command=lambda: self.show_frame(AboutUs))
		menubar.add_cascade(label="Help", menu=helpmenu)

		# display the menu
		self.config(menu=menubar)



	def show_frame(self, cont):

		frame = self.frames[cont]
		frame.tkraise()

# home section		
class HomeSection(Frame):

	def __init__(self, parent, controller):

		Frame.__init__(self,parent)

		#self.config(bg="red")

		height = 14
		width = 5

		cell = 0
		for a in range(width): #Columns
			cell = cell + 1
			b = Label(self, text=cell)
			b.grid(row=0, column=cell)

		cell = 0
		for b in range(height): #Columns
			cell = cell + 1
			b = Label(self, text=cell)
			b.grid(row=cell, column=0)

		for c in range(height): #Rows
		    for d in range(width): #Columns
		        b = Entry(self)
		        b.insert(c, c)
		        b.grid(row=c+1, column=d+1)

# projects
class Projects(Frame):

	def __init__(self, parent, controller):

		Frame.__init__(self, parent)
		self.config(bg="yellow")

		label = Label(self, text="Proyects page")
		label.pack(pady=10,padx=10)

# settings
class Settings(Frame):

	def __init__(self, parent, controller):
		
		Frame.__init__(self, parent)
		self.config(bg="blue")

		label = Label(self, text="Settings")
		label.pack(pady=10,padx=10)

# about
class AboutUs(Frame):

	def __init__(self, parent, controller):
		
		Frame.__init__(self, parent)
		frame = HtmlFrame(self,horizontal_scrollbar="auto")
		frame.grid(row=0,column=0)
		frame.set_content(open("help.html","r").read())
		frame.pack()


if __name__ == "__main__":
    app = MainContent()
    app.geometry("630x320")
    #app.overrideredirect(True)
    #app.geometry("{0}x{1}+0+0".format(app.winfo_screenwidth(), app.winfo_screenheight()))
    #app.focus_set()  # <-- move focus to this widget
    app.mainloop()

以上是关于python Tkinter上的多窗口的主要内容,如果未能解决你的问题,请参考以下文章

如何修复 python tkinter 上的按钮位置?

删除 Tkinter 窗口上的 TK 图标

试图按下主页上的应答按钮但窗口冻结:Tkinter

Python3 tkinter窗口中如何输入和输出

Python-Tkinter 组件之Toplevel

tkinter canvas create_window