【问题标题】:How can I fix my button positions on python tkinter?如何修复 python tkinter 上的按钮位置?
【发布时间】:2021-10-18 01:38:41
【问题描述】:

我想修复两个按钮的位置,例如,如果我调整窗口大小,按钮将保持在其位置。或者我的按钮会随着窗口大小动态调整大小。我已经使用 place 定位了我的按钮。但我没有成功。这是到目前为止的代码。

import turtle
import tkinter as tk

##wn=turtle.Screen()
wn=tk.Tk()
image=tk.PhotoImage(file="MS-3.PNG")
wn.geometry("700x700")

label1=tk.Label(wn,image=image)
label1.pack(side="top",fill="both",expand="yes")
label1.grid_rowconfigure(0, weight=1)
label1.grid_columnconfigure(0, weight=1)
label1.grid_rowconfigure(1, weight=1)


def callback1():
    import Detection1

def callback():
    import detection

button1=tk.Button(label1,text="Health Identification", command=callback, bd=12,bg="grey",font="caliber")

button1.place(x=100,y=500 )

label1.image=image
button2=tk.Button(label1,text="Disease Classification", command=callback1, bd=10,bg="grey",font="caliber")

button2.place(x=400, y=500 )

label1.image=image
wn.mainloop()

【问题讨论】:

    标签: python-3.x user-interface tkinter button tk


    【解决方案1】:

    Sadia:如果我正确理解了您的问题,您可以使用 wn.resizable(False, False) 使您的窗口无法调整大小。然后将您的按钮准确地放在您想要的位置。如果您是 tkinter 和 python 的新手,那么让每个对象都可以调整大小可能有点太复杂了。

    希望这会有所帮助。

    import turtle
    import tkinter as tk
    from PIL import Image, ImageTk
    
    ##wn=turtle.Screen()
    wn = tk.Tk()
    wn.geometry("700x700")
    wn.resizable(False, False)
    
    img = ImageTk.PhotoImage(Image.open("MS-3.PNG"))
    
    label1 = tk.Label(wn, image=img)
    label1.pack(side="top", fill="both", expand="yes")
    # label1.grid_rowconfigure(0, weight=1)
    # label1.grid_columnconfigure(0, weight=1)
    # label1.grid_rowconfigure(1, weight=1)
    
    
    def callback1():
        import Detection1
    
    
    def callback():
        import detection
    
    
    button1 = tk.Button(label1, text="Health Identification", command=callback, bd=12, bg="grey", font="caliber")
    button1.place(x=100, y=500)
    
    button2 = tk.Button(label1, text="Disease Classification", command=callback1, bd=10, bg="grey", font="caliber")
    button2.place(x=400, y=500)
    
    wn.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 2022-12-04
      • 2018-07-21
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 2016-06-14
      相关资源
      最近更新 更多