Tkinter - Изменение размера окна без изменения размеров фреймов / виджетов - PullRequest
0 голосов
/ 15 апреля 2020

Я создал графику для приложения Tkinter.

Вот как это выглядит enter image description here

Выглядит нормально, но в Y это очень большой размер. ось. Я бы хотел, чтобы окно было вырезано чуть ниже последнего кадра.

Так что я решил сделать это, изменив переменную HEIGHT, которая передается моему холсту. (700 -> 200)

К сожалению, размеры некоторых фреймов и виджетов изменяются, и это выглядит не очень хорошо.

Здесь вы можете увидеть результат.

enter image description here

Как видите, ось Y по-прежнему велика, но виджеты такие маленькие.

Есть ли способ просто "вырезать" окно прямо под последним кадром ? Без изменений?

Вот мой полный код:

import tkinter as tk
import tkinter.ttk as ttk
import serial.tools.list_ports


#to be used on our canvas
HEIGHT = 700
WIDTH = 800

# --- functions ---
def serial_ports():    
    return serial.tools.list_ports.comports()

def on_select(event=None):

    # get selection from event    
    print("event.widget:", event.widget.get())

    # or get selection directly from combobox
    print("comboboxes: ", cb.get())

def single_Sensor():    
    clicked_new = int(clicked)
# --- functions ---



# --- main ---
root = tk.Tk() #here we create our tkinter window
root.title("Sensor Interface")

#we use canvas as a placeholder, to get our initial screen size (we have defined HEIGHT and WIDTH)
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

#we use frames to organize all the widgets in the screen

# --- frame 1 ---
frame1 = tk.Frame(root)
frame1.place(relx=0, rely=0.05, relheight=0.03, relwidth=1, anchor='nw') #we use relheight and relwidth to fill whatever the parent is - in this case- root

label0 = tk.Label(frame1, text="Select the COM port that the device is plugged in: ")
label0.config(font=("TkDefaultFont", 8))
label0.place(relx = 0.1, rely=0.3, relwidth=0.3, relheight=0.5)


cb = ttk.Combobox(frame1, values=serial_ports())
cb.place(relx=0.5, rely=0.5, anchor='center')
# assign function to combobox
cb.bind('<<ComboboxSelected>>', on_select)
# --- frame 1 ---



# --- frame 2 ---
frame2 = tk.Frame(root, bd=5) #REMOVED THIS bg='#80c1ff' (i used it to see the borders of the frame)
frame2.place(relx=0, rely=0.1, relheight=0.07, relwidth=1, anchor='nw')

#button
button = tk.Button(frame2, text="Measure all Sensors", bg='#80c1ff', fg='red')  #bg='gray'
button.place(relx=0.2, rely=0.5, anchor='center')

#label
label1 = tk.Label(frame2, text="OR, select a single sensor to measure: ")
label1.config(font=("TkDefaultFont", 9))
label1.place(relx = 0.32, rely=0.3, relwidth=0.3, relheight=0.4)

#dropdown
OPTIONS = [0,1,2,3,4,5,6,7]
clicked = tk.StringVar()
clicked.set(OPTIONS[0]) # default value
drop = tk.OptionMenu(frame2, clicked, *OPTIONS)
drop.place(relx = 0.65, rely=0.25, relwidth=0.08, relheight=0.6)

dropDownButton = tk.Button(frame2, text="Measure this sensor", bg='#80c1ff', fg='red', command=single_Sensor) #bg='gray'
dropDownButton.place(relx = 0.85, rely=0.5, anchor='center')
# --- frame 2 ---



# --- frame 3 ---
frame3 = tk.Frame(root, bd=5) #REMOVED THIS bg='#80c1ff' (i used it to see the borders of the frame)
frame3.place(relx=0, rely=0.2, relheight=0.07, relwidth=1, anchor='nw')

stop_button = tk.Button(frame3, text="STOP measurement(s)", bg='#80c1ff', fg='red')
stop_button.place(relx=0.5, rely=0.5, anchor='center')
# --- frame 3 ---



# --- frame 4 ---
frame4 = tk.Frame(root, bd=5)
frame4.place(relx=0, rely=0.3, relheight=0.09, relwidth=1, anchor='nw')

label2 = tk.Label(frame4, text="Select a sensor to plot data: ")
label2.place(relx = 0.1, rely=0.3, relwidth=0.3, relheight=0.5)

clickedForPlotting = tk.StringVar()
clickedForPlotting.set(OPTIONS[0]) # default value
dropPlot = tk.OptionMenu(frame4, clickedForPlotting, *OPTIONS)
dropPlot.place(relx=0.5, rely=0.5, anchor='center')

dropDownButton = tk.Button(frame4, text="Plot sensor data", bg='#80c1ff', fg='red', command=single_Sensor) #bg='gray'
dropDownButton.place(relx = 0.85, rely=0.5, anchor='center')
# --- frame 4 ---


root.mainloop() #here we run our app
# --- main ---

1 Ответ

1 голос
/ 15 апреля 2020

Вы можете изменить размер холста, но для виджетов измените значение rely = 0.3! Вы видите, что у позиция относительная! к кадру хе-хе ... как если высота равна 100, то его у равен 30. Отношение равно независимо от высоты.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...