SaveText.Ru

Без имени
  1. import player_class
  2. import tkinter as tk
  3. from tkinter import ttk
  4. from PIL import Image, ImageTk
  5. from tkinter import filedialog
  6.  
  7.  
  8. class Window:
  9.     def __init__(self, title='Main',width = 600, height = 600):
  10.         self.width = width
  11.         self.height = height
  12.         self.size = f'{width}x{height}'
  13.         self.display = tk.Tk()
  14.         self.display.title(title)
  15.         self.display.geometry(self.size)
  16.         self.display.resizable(0,0)
  17.  
  18.     def start(self):
  19.             self.display.mainloop()
  20.  
  21. if __name__ == '__main__':
  22.     game = Window()
  23.     game.start()
  24.  
  25.  
  26. class Frame:
  27.     def __init__(self, window, title, background =    None):
  28.         self.frame = ttk.Frame(window.display, width = window.width, height = window.height)
  29.         self.title = title
  30.  
  31.         self.style = ttk.Style()
  32.         self.style.configure('Tlabel', font=('Colibri', 14))
  33.         self.set_background_image(background)
  34.         self.frame.grid(row=0, column=0, sticky='nsew')
  35.  
  36.     def activate(self):
  37.         self.frame.tkraise()
  38.  
  39.     def change_title(self):
  40.         self.frame.master.title(self.title)
  41.  
  42.     def set_background_image(self, background):
  43.         if background:
  44.             image = ImageTk.PhotoImage(Image.open(background).resize((self.window.width, self.window.height), Image.ANTIALIAS))
  45.             background_label = ttk.Label(self.frame, image=image)
  46.             background_label.place(x=0,y=0)
  47.  
  48. class CreatePlayerFrame(Frame):
  49.     def __init__(self, window, title, background = None):
  50.         super().__init__(window, title, background)
  51.  
  52.         name_label = ttk.Label(self.frame, text='Player name: ')
  53.         name_label.place(relx =0.01, rely=0.01,anchor = tk.NW)
  54.         self.name_field = ttk.Entry(self.frame)
  55.         self.name_field_place(relx=0.01, rely=0.05, anchor=tk.NW)
  56.  
  57.  
  58.         age_label = ttk.Label(self.frame, text='Player age: ')
  59.         age_label.place(relx =0.01, rely=0.1,anchor = tk.NW)
  60.         self.age_field = ttk.Entry(self.frame)
  61.         self.age_field_place(relx=0.01, rely=0.15, anchor=tk.NW)
  62.  
  63.  
  64.         gender_label = ttk.Label(self.frame, text='Player gender: ')
  65.         gender_label.place(relx =0.01, rely=0.2,anchor = tk.NW)
  66.         self.gender_field = ttk.Combobox(self.frame, value=['Male', 'Female'])
  67.         self.gender_field_place(relx=0.01, rely=0.25, anchor=tk.NW)
  68.  
  69.  
  70.         photo_label = ttk.Label(self.frame, text='Player photo: ')
  71.         photo_label.place(relx =0.4, rely=0.01,anchor = tk.NW)
  72.  
  73.         self.img_path = 'Image/Meme17.jpg'
  74.         photo = ImageTk.PhotoImage(Image.open(self.img_path).resize((350,350), Image.ANTIALIAS))
  75.         self.photo = ttk.Label(self.frame, image = photo)
  76.         self.photo.place(rex=0.4,rely=0.05, anchor=tk.NW)
  77.  
  78.     def choose_photo(self):
  79.         self.img_path = filedialog.askopenfilename(
  80.             master=self.frame, title = 'Select player photo', filetypes=(('Image files', '*.png *.jpg'),)
  81.         )
  82.         photo = ImageTk.PhotoImage(Image.open(self.img_path).resize((350, 350), Image.ANTIALIAS
  83.         self.photo.configure(image=photo)
  84.         self.photo.image = photo
  85.  
  86.     def create_player(self):
  87.             player = player_class.Player(
  88.                 self.name_field.get(), int(self.age_field.get()), self.gender_field.get(), self.img_path
  89.             )
  90.  

Share with your friends:

Print