Can anyone help me why columnspan not having any effect in this code:
import tkinter as tk
root = tk.Tk()
frametop = tk.Frame(root).grid(row = 0, column = 0, columnspan = 2)
labeltest1 = tk.Label(frametop, text="A").grid(row = 0, column = 0)
labeltest2 = tk.Label(frametop, text="B").grid(row = 0, column = 1)
labeltest3 = tk.Label(frametop, text="C").grid(row = 0, column = 2)
framebottom = tk.Frame(root).grid(row = 1, column = 0)
labeltest4 = tk.Label(framebottom, text="Hello World").grid(row = 1, column = 0)
labeltest5 = tk.Label(framebottom, text="Hello World").grid(row = 1, column = 1)
labeltest6 = tk.Label(framebottom, text="Hello World").grid(row = 1, column = 2)
root.mainloop()
labeltest1, labeltest2, labeltest3 are being distributed in column similar to the widgets of framebottom. What I wanted is a grid of 3 columns inside a frame with a columnspan = 2 with the same row.
Something similar to this:
|| A | B | C ||
| Hello World | Hello World | Hello World |
The way your code is organized, the Frames have no effect; when you grid on the same line as the widget creation, None gets returned and stored in the variables you are using for the Frames, and Label. A consequence is that the labels are inserted directly into root.
The following does the same, with a placement of the labels per your request.
import tkinter as tk
root = tk.Tk()
tk.Label(root, text="A").grid(row=0, column=0, columnspan=2)
tk.Label(root, text="B").grid(row=0, column=2, columnspan=2)
tk.Label(root, text="C").grid(row=0, column=4, columnspan=2)
tk.Label(root, text="Hello World").grid(row=1, column=0, columnspan=3)
tk.Label(root, text="Hello World").grid(row=1, column=3, columnspan=3)
tk.Label(root, text="Hello World").grid(row=1, column=6, columnspan=3)
root.mainloop()
GUI appearance (osX):
Related
I'm studying GUI, so please understand my poor codes below.
I was trying to make a program which gets game-character's information. So if you press the 'search' button, the list would be shown below. But... it only shows about 11 names due to the window size. So i wanted to put a scrollbar for that area, but I just don't know how to link the scroll bar to control the area. I meant, the scroll bar itself has created, and it does scroll itself, but it doesn't scroll the window I wanted. I must have linked it wrong but... not sure.
Below is the minimized example code, but it's still quite long and crude. Sorry for that again.
If anyone can enlighten me, it would be really great help for this and my future works as well.
import tkinter as tk
import requests
from bs4 import BeautifulSoup
import webbrowser
import time
global var_dict, input_id, output
var_dict = {}
def enter_call_back(event=None):
output.grid(column = 0, row = 2, columnspan = 5 , sticky='w')
output.insert(tk.END,"Text_Something")
output.update()
search_chr()
def open_browse(url_list):
for url in url_list:
time.sleep(0.3)
webbrowser.open(url)
def search_inven(ch_id):
if ch_id == "ch1" or ch_id == "ch13" or ch_id == "ch15" :
num = 5
url_list = ["something.url","something2.url"]
self_count = 1
else:
num = 0
url_list = []
self_count = 0
masterset = []
masterset.append(num)
masterset.append(url_list)
masterset.append(self_count)
return masterset
def search_chr():
global var_dict, output
for things in var_dict.keys():
var_dict[things].destroy()
chr_list = ["ch1","ch2","ch3","ch4","ch5","ch6","ch7","ch8","ch9","ch9","ch10","ch11","ch12","ch13","ch14","ch15"]
output.insert(tk.END," Done! \n\n")
var_dict = {}
num = -1
for ch in chr_list:
num += 1
var_dict["output%s" %num] = tk.Entry(frame_buttons, width = 125)
result = search_inven(ch)
if result[0] == 0:
var_dict["output"+str(num)].insert(0, "Clean "+ch+"\n")
var_dict["output"+str(num)].grid(column = 0, row = num, sticky='w', padx=5, pady=5)
else:
url_list = result[1]
var_dict["o-button%s" %num] = tk.Button(frame_buttons, command=lambda url_list = url_list : open_browse(url_list))
var_dict["o-button"+str(num)].grid(column = 1, row = num, sticky='e')
var_dict["o-button"+str(num)].config(text="URL")
var_dict["output"+str(num)].insert(0, "Not Clean "+str(result[0])+" Self : "+str(result[2])+" Ch_id : "+ch+")\n")
var_dict["output"+str(num)].grid(column = 0, row = num, sticky='w', padx=5, pady=5)
vsb = tk.Scrollbar(frame_canvas, orient="vertical")
vsb.grid(row=0, column=1, sticky='ns')
vsb.config(command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
frame_canvas.config(height = 300)
canvas.config(scrollregion=canvas.bbox("all"))
root = tk.Tk()
root.geometry("760x710")
root.resizable(width=False, height=False)
root.title("Minimum v.1.2")
root.grid_rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
frame_main = tk.Frame(root, bg="gray")
frame_main.grid(sticky='news')
intro = tk.Text(frame_main, height = 17, bg="#E3D5F3")
intro.option_add("*Font", "명조 10")
intro.insert(tk.CURRENT,"Text_something")
intro.config(state='disabled')
intro.grid(row=0, column=0, pady=(5, 0), columnspan = 5, sticky='nw')
input_id = tk.Entry(frame_main, width = 35)
input_id.option_add("*Font","명조 10")
input_id.insert(0,"Ch_name")
input_id.grid(row=1, column=0, pady=(5, 0), sticky='w')
search_btn = tk.Button(frame_main)
search_btn.config(text="Search")
search_btn.option_add("*Font","명조 10")
search_btn.config(width=5,height=1)
search_btn.grid(row = 1, column = 0, pady=(5, 0), sticky='e')
output = tk.Text(frame_main, height = 10)
output.option_add("*Font","명조 10")
output.grid(row = 2, column = 0,pady=(5,0),sticky='nw')
frame_canvas = tk.Frame(frame_main, width = 565)
frame_canvas.grid(row=3, column=0, pady=(5, 0), columnspan = 3 ,sticky='nw')
frame_canvas.grid_rowconfigure(0, weight=1)
frame_canvas.grid_columnconfigure(0, weight=1)
frame_canvas.grid_propagate(False)
canvas = tk.Canvas(frame_canvas, bg="gray", height=500,scrollregion=(0,0,500,1800))
canvas.grid(row=0, column=0, sticky="news")
frame_buttons = tk.Frame(canvas, bg="gray")
frame_buttons.grid(row = 0, column = 0,sticky='e')
root.bind('<Return>',enter_call_back)
search_btn.config(command = enter_call_back)
root.mainloop()
First, using grid() to put frame_buttons into the canvas will not affect the scrollregion of the canvas. Use canvas.create_window() instead.
Second, it is better to bind <Configure> event on frame_buttons and update canvas' scrollregion inside the bind callback. Then adding widgets to frame_buttons will automatically update the scrollregion.
Also note that you have created new scrollbar and put it at same position whenever search_chr() is executed. Better create the scrollbar only once outside the function.
I would like print all the information present in the excel data in new tkinter window
You can use Tkinter's grid.
For example, to create a simple excel-like table:
from Tkinter import *
root = Tk()
height = 5
width = 5
for i in range(height): #Rows
for j in range(width): #Columns
b = Entry(root, text="")
b.grid(row=i, column=j)
mainloop()
To print, consider the following example in which I make a button with Tkinter that gets some text from a widget and then prints it to console using the print() function.
from tkinter import *
from tkinter import ttk
def print_text(*args):
try:
print(text1.get())
except ValueError:
pass
root = Tk()
root.title("Little tkinter app for printing")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column = 0, row = 0, sticky = (N,W,E,S))
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
text1 = StringVar()
text_entry = ttk.Entry(mainframe, width = 20, textvariable=text1)
text_entry.grid(column = 1, row = 2, sticky = (N,W,E,S))
ttk.Button(mainframe, text = "Print!", command =
print_text(text1)).grid(column = 1, row = 3, sticky = (E))
for child in mainframe.winfo_children():
child.grid_configure(padx = 5, pady = 5)
text_entry.focus()
root.bind('<Return>', print_text)
root.mainloop()
I am using tkinter and created a label with textvariable. What I want is to have let say top 5 customer listed in this one label. The customer list is from an excel file.
Part of my code:
import pandas as pd
import tkinter as tk
from tkinter import ttk
df = pd.read_excel('Sample.xlsx', "Sheet1")
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(1, weight=1)
self.grid()
self.baseDown = tk.Frame(root, width=800, height = 400, background='#E6E6FA', relief = "groove", borderwidth = 2)
self.baseDown.grid(columnspan=7, row=1, rowspan = 4, pady = 1, padx = 2, sticky="wn")
self.BDown = tk.LabelFrame(self.baseDown, text = " Analysis ", width=800, height=400, background ='#E6E6FA', relief = 'ridge', borderwidth = 2)
self.BDown.config(font = ('Verdana', 8, 'bold'))
self.BDown.grid(columnspan=7, row=1, rowspan = 4, pady = 3, padx = 2, sticky="wn")
self.baseDown.grid()
self.cust_var = tk.StringVar()
self.lbl_custname = ttk.Label(self.BDown, textvariable = self.cust_var)
self.lbl_custname.config(font=('Segoe UI', 8), width = 100)
self.lbl_custname.grid(row=1, column=1, columnspan=3, pady = 1, padx = 2, sticky="nw")
top = df.nlargest(5, columns=['Value'])
for index, row in top.iterrows():
self.cust_var.set(row[top['Customer Name'].values.astype(str)])
root = tk.Tk()
root.configure(background = 'white')
#root.resizable(False, False)
app = Application(master=root)
app.mainloop()
The output is as below:
Customer1 NaN
Customer2 NaN
Customer3 NaN
Customer4 NaN
Customer5 NaN
Name: 20371, dType: Object
Customer list is correct, but I don't understand why NaN is there and why is the datatype appearing as well.
My excel file only carry 2 columns:
Customer Name
Value
You are setting the self.cust_var to the result of row[top['CustomerName'].values.astype(str)] 5 times.
If you want to show what you expect, you simply set the variable once (replace the for loop):
self.cust_var.set('\n'.join(top['Customer Name'].values))
How do I write user input from tkinter entry widget to xlsx?
Should I be using a different module?
from tkinter import *
import xlsxwriter
def output():
c = Toplevel(root)
c.title =("Main")
c.geometry =('')
e= Entry(c, width=20).grid(row=0, column=1, sticky=W, padx=10)
l=Label(c, text="Current Value").grid(row = 0, column=0, sticky=W, padx=10)
e2 = Entry(c, width=20).grid(row=1, column=1, sticky=W, padx=10)
l2= Label(c, text="New Value").grid(row = 1, column=0, sticky=W, padx=10)
b=Button(c, text="Submit",command= write_to_xlsx).grid(row= 1, column=2, sticky=E, padx=10)
def write_to_xlsx():
workbook =xlsxwriter.Workbook('tkintertest18.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write_string('C1', 'e2.get()')
workbook.close()
root = Tk()
root.title("Main Menu")
root.geometry('400x400+230+130')
Button(root, text="1", command=output).grid(row =0, column= 2)
root.mainloop()
You should almost always use a class when tkinter is involved. So here is a working prototype of your code above. There were multiple problems. One is that when you slap grid next to your widgets instead of putting them on a new line, grid returns None, not the object. Implementing the app as a class enables the other functions to access the GUI components (in this case self.e2). Another error was you had single-quoted the 'e2.get()' call, which is technically a string. So removing the quotes fixes that. There may have been other things...
import xlsxwriter
class XLwriter:
def __init__(self):
c = Toplevel(root)
c.title =("Main")
c.geometry =('')
self.e= Entry(c, width=20).grid(row=0, column=1, sticky=W, padx=10)
self.l=Label(c, text="Current Value").grid(row = 0, column=0, sticky=W, padx=10)
self.e2 = Entry(c, width=20)
self.e2.grid(row=1, column=1, sticky=W, padx=10)
self.l2= Label(c, text="New Value").grid(row = 1, column=0, sticky=W, padx=10)
self.b=Button(c, text="Submit",command=self.write_to_xlsx).grid(row= 1, column=2, sticky=E, padx=10)
def write_to_xlsx(self):
workbook =xlsxwriter.Workbook('tkintertest18.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write_string('C1', self.e2.get())
workbook.close()
root = Tk()
root.title("Main Menu")
root.geometry('400x400+230+130')
app = XLwriter()
#Button(root, text="1", command=output).grid(row =0, column= 2)
root.mainloop()
Seeing that Ron already provided you with a class to perform this task I will respond with the non-class method of doing this to more closely reflect the current code you have.
Keep in mind that with xlsxwriter there is not a way to read from the spreadsheet so for the entry field you have that is labeled as "Current Value" you will need to use a different excel library to fill that field with data from the spreadsheet.
Make sure if you are going to interact with a widget that you do not use the geometry manager directly with the creation of the widget. This will return None to anything trying to interact with it. Instead on the next line after you have created the widget you can use the widget variable name and then your geometry manager of choice(grid(), pack(), place()). This will allow us to interact with the widget without the issue of None being returned by the geometry manager.
In order for you to be able to interact with e2 outside of the output() function you will need to assign e2 to the global namespace. You can do this by simply adding global e2 inside your output() function.
Note: The use of global is best avoided whenever possible this is one compelling reason to use a more Object Oriented Programing method (class) as the use of global is not needed when using class attributes.
from tkinter import *
import xlsxwriter
root = Tk()
root.title("Main Menu")
#root.geometry('400x400+230+130')
def output():
global e2
c = Toplevel(root)
c.title = ("Main")
e = Entry(c, width = 20)
e.grid(row = 0, column = 1, sticky = W, padx = 10)
l = Label(c, text = "Current Value")
l.grid(row = 0, column = 0, sticky = W, padx = 10)
e2 = Entry(c, width=20)
e2.grid(row = 1, column = 1, sticky = W, padx = 10)
l2 = Label(c, text = "New Value")
l2.grid(row = 1, column = 0, sticky = W, padx = 10)
b = Button(c, text = "Submit",command = write_to_xlsx)
b.grid(row = 1, column = 2, sticky = E, padx = 10)
def write_to_xlsx():
workbook = xlsxwriter.Workbook('tkintertest18.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write_string('C1', e2.get())
workbook.close()
btn = Button(root, text = "1", command = output)
btn.grid(row = 0, column = 2)
root.mainloop()
def mouse_click(event):
#'reports' to both terminal and results text box
parent_name = Frame.winfo_parent(root)
parent = Frame._nametowidget(parent_name)
result = ("{0}: {1},{2}\n ".format('Clicked at', event.x, event.y))
print('clicked at',parent, event.x, event.y)
from tkinter import *
root = Tk()
root.title("Change Text")
root.geometry('700x500')
top=root.winfo_toplevel()
for rn in range(0,9): # rn = row number
top.rowconfigure(rn, weight=1)
top.rowconfigure(rn, weight=0)
for cn in range(0,5): # cn = column number
top.columnconfigure(cn, weight=1)
# FRAME 1
frame1 = Frame(root, borderwidth = 2, background = '#EFE0CD', relief = RIDGE,width = 25, height = 20)
frame1.bind("<Button-1>", mouse_click)
frame1.grid(column = 0, row = 0, columnspan = 2, rowspan = 3, sticky = N+S+E+W)
frame1_lbl = Label(frame1, text='Frame 1', font='comic-sans-MS 10 ', fg ='red', bg = '#EFE0CD')
frame1_lbl.grid(row=0, column =0)
# FRAME 2
frame2 = Frame(root, borderwidth = 2, background = '#CCC6B0', relief = RIDGE,width = 25, height = 20)
frame2.bind("<Button-1>", mouse_click)
frame2.grid(column = 0, row = 3, columnspan = 2, rowspan = 3, sticky = N+S+E+W)
frame2_lbl = Label(frame2, text='Frame 2', font='comic-sans-MS 10', fg ='red', bg = '#CCC6B0')
frame2_lbl.grid(row=0, column =0,)
root.mainloop()
I know that this is a really dumb question. How do I identify the Frame in which the mouse is clicked so that report prints clicked frame1 or frame2 at x y coordinates?. I found a bit about using winfo_parent but have obviously not used it properly.
Thank you
The event object that is passed in to the callback has a widget attribute which is a reference to the widget that got the event. So, event.widget is what you want to print.