Wie sieht es gerade aus < /p>
Kann mir jemand helfen, die Kästchen horizontal und vertikal auszurichten? Im Bild sind das Nachrichteneintragsfeld und die anderen 4 Felder nicht ausgerichtet. Das Meldungseintragsfeld sollte die gleiche Länge wie Frame1 und Frame2 haben. Und die Kisten in den Frames haben unterschiedliche Längen. Sie sollten die gleiche Länge sein und mit den roten Linien ausgerichtet sein. Zum Beispiel sollte es so aussehen wie dieses Bild: Wie ich möchte, dass es aussieht < /p>
Dies ist der genaue Code, den ich habe: < /p>
import tkinter as tk
from tkinter import ttk, filedialog
from PIL import Image, ImageTk
import sys
import os
root = tk.Tk(className="Chat APP")
root.title("Chat APP")
# Maximize the window
root.attributes('-zoomed', True)
# Get the screen width and height
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
new_note_icon = Image.open("new_note.jpg")
new_note_icon = new_note_icon.resize((25, 25))
new_note_icon = ImageTk.PhotoImage(new_note_icon)
# Proportional calculations
def proportion_width(p):
return int(screen_width * p)
def proportion_height(p):
return int(screen_height * p)
chat_frame = tk.Frame(root, bg='white')
chat_frame.pack(expand=True, fill=tk.BOTH)
chat_frame.grid_rowconfigure(0, weight=0)
chat_frame.grid_rowconfigure(1, weight=1)
chat_frame.grid_columnconfigure(0, weight=1)
chat_log = tk.Text(chat_frame, state='disabled', wrap='word', width=70, height=15,
font=('Helvetica', 14), bg="white", fg="black", highlightthickness=0, borderwidth=0)
chat_log.grid(row=1, column=0, sticky='NESW', padx=proportion_width(0.275), pady=proportion_height(0.01))
# Create a scrollbar for the chat log
scrollbar = tk.Scrollbar(chat_frame, command=chat_log.yview)
scrollbar.grid(row=1, column=1, sticky='NS')
chat_log['yscrollcommand'] = scrollbar.set
frame1 = tk.Frame(root, bg="white")
frame1.pack(side=tk.TOP, pady=(proportion_height(0.1), 1))
frame2 = tk.Frame(root, bg="white")
frame2.pack(side=tk.TOP, pady=(proportion_height(0.005), proportion_height(0.02)))
print("Width is",chat_frame.winfo_width())
chat_with_pdf_button = ttk.Button(frame1, width=proportion_width(0.02), text="Chat with PDF", style="def2.TButton",)
chat_with_pdf_button.pack(side=tk.LEFT, padx=(proportion_width(0.0843),0), ipady=proportion_height(0.01))
# Create a button to trigger the summarization process
summarize_button = ttk.Button(frame1, width=proportion_width(0.02), text="Chat with a YouTube video", style="def2.TButton")
summarize_button.pack(side=tk.LEFT, padx=(proportion_height(0.01), 0), ipady=proportion_height(0.01))
letter_button = ttk.Button(frame2, width=proportion_width(0.02), text="Write a letter", style="def2.TButton")
letter_button.pack(side=tk.LEFT, padx=(proportion_width(0.0843),0), ipady=proportion_height(0.01))
blog_button = ttk.Button(frame2, width=proportion_width(0.02), text="Chat with a Blog Post", style="def2.TButton")
blog_button.pack(side=tk.LEFT, padx=(proportion_height(0.01), 0), ipady=proportion_height(0.01))
new_note_button = ttk.Button(chat_frame, style="Toggle.TButton", image=new_note_icon)
new_note_button.grid(row=0, column=0, sticky='W', padx=(proportion_width(0.272), 0), pady=(proportion_height(0.01), 0))
# Frame to contain message_entry and buttons
frame_for_widgets = tk.Frame(root, bg="white")
frame_for_widgets.pack(side=tk.BOTTOM)
empty_space = tk.Text(
frame_for_widgets, # Use frame2 as the parent widget
width=proportion_width(0.001), # Adjusting padx proportionally
bg="white",
borderwidth=0,
# Removing fixed width and height, so it expands with the frame
height=proportion_width(0.000001),
# Set a fixed height if needed
spacing1=proportion_height(0.02), # Adjust spacing1 proportionally
spacing3=proportion_height(0.02), # Adjust spacing3 proportionally
font=('Helvetica', 14)
)
message_entry = tk.Text(
frame_for_widgets, # Use frame2 as the parent widget
width=proportion_width(0.0495), # Adjusting padx proportionally
bg="white",
insertbackground='white',
fg="white",
borderwidth=0,
# Removing fixed width and height, so it expands with the frame
height=proportion_width(0.000001),
# Set a fixed height if needed
spacing1=proportion_height(0.02), # Adjust spacing1 proportionally
spacing3=proportion_height(0.02), # Adjust spacing3 proportionally
font=('Helvetica', 14)
)
# Pack message_entry into frame2
message_entry.pack(
side=tk.LEFT,
padx=(proportion_width(0.208),0),
#fill=tk.X, # Fill horizontally
pady=(0, proportion_height(0.02))
#padx=(0),
# Adjusting padx proportionally
# Adjusting pady proportionally
)
message_entry.mark_set("insert", "%d.%d" % (0,0))
send_icon = Image.open("send_icon.png")
send_icon = send_icon.resize((55, 55))
send_icon = ImageTk.PhotoImage(send_icon)
# Send button
send_button = ttk.Button(frame_for_widgets, image=send_icon, style="Send.TButton")
send_button.image = send_icon
send_button.pack(side=tk.LEFT, pady=(0, proportion_height(0.02)), padx=(1,0),)
message_entry.focus_set()
root.mainloop()
< /code>
Das Problem ist, dass diese Ausrichtungen für verschiedene Bildschirmgrößen unterschiedlich aussehen. Es sollte für alle Bildschirmgrößen ausgerichtet sein; unabhängig von der Bildschirmgröße des Computers.
Müssen die Kisten horizontal und vertikal in Python Tkinter ausrichten ⇐ Python
-
- Similar Topics
- Replies
- Views
- Last post