mukesh krishnan
mukesh krishnan

Reputation: 353

How to make Tkinter button to be placed in particular position?

I am new to python so I was trying to make a GUI, in that I have to place a button in a particular position.

I tried using self.nxt_form.place(x=200,y=100) instead of self.nxt_form.pack().

But the button disappeared and only the frame appeared when it ran. Can you tell me how to place the button in a particular position?

Here is the code:

import tkinter as tk

class Main_form:
def __init__(self, root,title="Simulated MTBF"):
self.root = root
    self.frame = tk.Frame(self.root)
    """Button nxt_form which moves to next form"""
    self.nxt_form = tk.Button(self.frame, text = 'Next Form', width = 25,command = self.new_window)
    self.nxt_form.pack()
    self.frame.pack()
     """command to open new window by clicking Button """
def new_window(self):
    self.newWindow = tk.Toplevel(self.root)
    self.app = Demo2(self.newWindow)

 class Demo2:
        def __init__(self, root):
    self.root = root
    self.frame = tk.Frame(self.root)
    self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
    self.quitButton.pack()
    self.frame.pack()
def close_windows(self):
    self.root.destroy()

def main(): 
root = tk.Tk()
app = Main_form(root)
root.mainloop()

if __name__ == '__main__':
main()

Upvotes: 1

Views: 6070

Answers (1)

ryanmoir
ryanmoir

Reputation: 229

when i am using tkinter i used column and row to position objects

self.btn = tk.Button(self, text = "button")
self.btn.grid(row = 1, column = 1)

EDIT - expanded on information in response to comment (below)

I would make an label and change its width and height to make the spacing you need (note im a beginer at python as well so this is probly a bad way but it works)

from tkinter import *
import tkinter as tk
from tkinter.ttk import Combobox,Treeview,Scrollbar

class MainMenu(Frame):
    def __init__(self, master):
        """ Initialize the frame. """
        super(MainMenu, self).__init__(master)
        self.grid()
        self.create_GUI()

    def create_GUI(self):
        frame1 = tk.LabelFrame(self, text="frame1", width=300, height=130, bd=5)
        frame1.grid(row=0, column=0, columnspan=3, padx=8)
        #the frame is not needed but it is a good thing to use as can group
        #parts of your interface together

        self.text1 = Entry(frame1)
        #note if you were not using frames would just put self here
        self.text1.grid(row = 1, column = 0)

        self.text2 = Label(frame1, text = "",height = 10)
        self.text2.grid(row = 2 , column = 0)

        self.text3 = Entry(frame1)
        self.text3.grid(row = 3, column = 0)



root = Tk()
root.title("hi")
root.geometry("500x500")
root.configure(bg="white")
app = MainMenu(root)
root.mainloop()

Also note that you can not use pack and grid together what you could do is group your objects in different frames then use grid in one frame and pack in a different frame. I personally prefer to use grid to pack as it gives you more control over your object then pack does

Upvotes: 1

Related Questions