Pittawas Kijoran
Pittawas Kijoran

Reputation: 3

Python tkinter cannot print in same line?

So I am currently writing a program using python Tkinter where I enter details and it will enter each task on the same line. I left a photo of what I have done down below. I would like to have all of them print out on the same line.

Click to see my output so far

I would like to have both 2s and the three buttons in the same line.

Here's my code

# This Python file uses the following encoding: utf-8
import os, sys
import tkinter as tk
from tkinter import *
from tkinter import ttk
import tkinter.font
import tkinter.messagebox

#Set up window
root = tk.Tk()
root.title("SNKRS Bot")
root.geometry("1000x600")

#Enter ID
IDHead = Label(root, text="ID:")
IDHead.grid(row=1, column=0)
IDInput = Entry(root, textvariable="", width='5')
IDInput.grid(row=1, column=1)

#Enter link
linkHead = Label(root, text="Link:")
linkHead.grid(row=2, column=0)
linkInput = Entry(root, textvariable="", width='60')
linkInput.grid(row=2, column=1)

ID = []
linkList = []

def createTask():

    #Create variables for inputs
    linkInput1 = linkInput.get()
    IDInput1 = IDInput.get()

    #Append to lists
    linkList.append(linkInput1)
    ID.append(IDInput1)

    #print lists to check
    print("ID: " + str(ID))
    print("Links: " + str(linkList))

    #Clear inputs
    IDInput.delete(0, END)
    linkInput.delete(0, END)

    #Output values
    # Label(root, text=(IDInput1 + " | " + linkInput1)).grid(column=0)
    Label(root, text=linkInput1).grid(column=0)
    Label(root, text=linkInput1).grid(column=1)

    #Actions for each task
    def startTask():
        print("Task started")
    def stopTask():
        print("Task stopped")
    def deleteTask():
        print("Task deleted")

    #Buttons for actions
    startButton = tk.Button(root, text="start", command=startTask).grid(column=2)
    stopButton = tk.Button(root, text="stop", command=stopTask).grid(column=3)
    deleteButton = tk.Button(root, text="delete", command=deleteTask).grid(column=4)

#Create task
create = tk.Button(root, text="Create task", command=createTask)
create.grid(row=3, column=1)

root.mainloop()

Upvotes: 0

Views: 511

Answers (2)

toyota Supra
toyota Supra

Reputation: 4537

I would like to have both two labels and the three buttons in the same line.

Your grid manager is messed up. Better start at row 0 instead of 1.

Change this to:

  • On line 15, IDHead.grid(row=0, column=0)
  • On line 17, IDInput.grid(row=0, column=1)
  • On line 21, linkHead.grid(row=1, column=0)
  • On line 23, linkInput.grid(row=1, column=1)
  • On line 48, Label(root, text=linkInput1).grid(row=3,column=0 )
  • On line 49, Label(root, text=linkInput1).grid(row=3,column=1)
  • On line 60, startButton = tk.Button(...).grid(row=3,column=2)
  • On line 61, stopButton = tk.Button(...).grid(row=3,column=3)
  • On line 62, deleteButton = tk.Button(...).grid(row=3,column=4)

Screenshot:

enter image description here

Upvotes: 0

CHARAN REDDY
CHARAN REDDY

Reputation: 11

Whenever you are packing a label,in the grid method you are passing only the column.by doing so,it will pack the widget in a new row. To avoid that pass both the row and column arguments.

Ex: Label(root,text="some text").grid(row=3,column=0)

Upvotes: 1

Related Questions