How to create large extry box in python tkinter?
I have tried to use height in ttk.entry() but the error show :
_tkinter.TclError: unknown option "-height"
from tkinter import *
from tkinter import ttk
GUI = Tk()
GUI.title("myTest")
GUI.geometry("700x700")
S_NOTE = StringVar()
E_NOTE = ttk.Entry(GUI, textvariable = S_NOTE, font = FONT1, width = 40, height = 20)
E_NOTE.grid(row = 0, column = 0, columnspan = 2, rowspan = 2)
GUI.mainloop()
I also need to get the StringVar from the entrybox and fix the position (such as using grid)
Looks like you are using a bad way to do this..
see.. You can use the Text widget to do the same..
Example:
from tkinter import *
GUI = Tk()
GUI.title("myTest")
GUI.geometry("700x700")
def set_text_to_variable():
global E_NOTE
global S_NOTE
S_NOTE = E_NOTE.get(1.0,END)
print("S_NOTE = ",S_NOTE)
E_NOTE = Text(GUI, font = "Segoe", width = 40, height = 20)
E_NOTE.grid(row = 0, column = 0, columnspan = 2, rowspan = 2)
Change_variable = Button(GUI, text = "CHANGE THE \"S_NOTE\" VARIABLE", command = set_text_to_variable)
Change_variable.grid()
GUI.mainloop()
Related
I've made this converter. I want it to convert decimal values into binary and hexadecimal using Tkinter..
I made a text input box but I don't know how to get the input values from the input box.
We aren't supposed to use OOP so we can't use classes.
Here is my code (it's in french for some parts) :
import tkinter as tk
from tkinter import *
def ConverterWind():
convertisseur = Tk()
convertisseur.title("Convertisseur")
inputZone = Text(convertisseur, height=2, width=50)
inputZone.pack()
getTextArea = Button(convertisseur, text = "Convertir !", command = getText)
getTextArea.pack()
convertisseur.mainloop
MainMenu = Tk()
MainMenu.title("Choix de Modes")
button1 = Button(MainMenu, text = "convertisseur", command = ConverterWind)
button1.pack(side = LEFT, padx= 10, pady = 10)
button2 = Button(MainMenu, text = "QUITTER", command = MainMenu.destroy)
button2.pack(side = RIGHT, padx= 10, pady = 10)
MainMenu.mainloop()
You can use the Text.get(Index1,Index2) to get the text from Text widget
Try this.
import tkinter as tk
from tkinter import *
def getText(inputText):
print(inputText.get(1.0,END))
def ConverterWind():
convertisseur = Tk()
convertisseur.title("Convertisseur")
inputZone = Text(convertisseur, height=2, width=50)
inputZone.pack()
getTextArea = Button(convertisseur, text = "Convertir !", command = lambda: getText(inputZone))
getTextArea.pack()
convertisseur.mainloop
MainMenu = Tk()
MainMenu.title("Choix de Modes")
button1 = Button(MainMenu, text = "convertisseur", command = ConverterWind)
button1.pack(side = LEFT, padx= 10, pady = 10)
button2 = Button(MainMenu, text = "QUITTER", command = MainMenu.destroy)
button2.pack(side = RIGHT, padx= 10, pady = 10)
MainMenu.mainloop()
Application: Building a notes application (as an intro to GUI development in Python) which includes feature of a scrollbar to scroll through a textbox
Problem: I can't actually seem to scroll down through the textbox. I don't seem to get the grayed rectangle which lets me control the scrollbar and scroll up/down through the textbox
#importing necessary packages
from tkinter import *
from tkinter import font
from tkinter import ttk
#set up main window
root = Tk()
root.title("Notes")
root.geometry("400x650")
#functions
#functions to change all widget button's backgrounds when user hovers over it and leaves it
def enter_button(e):
e.widget.config(background = "#D4D4D4")
#SystemButtonFace is default colour
def leave_button(e):
e.widget.config(background = "SystemButtonFace")
#clear text in text-box
def clear():
#delete all text from text_box
text_box.delete(1.0,END)
def bold_it():
#create font
try:
bold_font = font.Font(text_box, text_box.cget("font"))
bold_font.configure(weight = "bold")
#creating tag called "bold" which bolds textll upon condition
text_box.tag_configure("bold", font = bold_font)
#creating a bold tag which highlights first character
bold_tag = text_box.tag_names("sel.first")
#condition for checking to see if tag is applied or not
#in the first highlighted character
#if tag is applied, remove the bold from first-highlighted text
#- last highlighted text
#"bold" needs to be matched in the tag
if "bold" in bold_tag:
text_box.tag_remove("bold","sel.first","sel.last")
else:
text_box.tag_add("bold","sel.first", "sel.last")
except TclError:
pass
def italics_it():
try:
#create a font
italics_font = font.Font(text_box, text_box.cget("font"))
italics_font.configure(slant = "italic")
#create a tag called "italic"
text_box.tag_configure("italics", font = italics_font)
italics_tag = text_box.tag_names("sel.first")
#condition to see whether tag has been applies or not
if "italics" in italics_tag:
text_box.tag_remove("italics", "sel.first","sel.last")
else:
text_box.tag_add("italics", "sel.first", "sel.last")
except TclError:
pass
#frames
top_frame = LabelFrame(root, padx = 30, pady = 10)
button_frame = LabelFrame(root, padx = 30, pady = 10)
text_frame = LabelFrame(root, padx = 10, pady = 10)
bottom_frame = LabelFrame(root, borderwidth = 0, highlightthickness = 5)
top_frame.grid(row = 0 , column = 0)
button_frame.grid(row = 1, column = 0, pady = 10)
text_frame.grid(row = 2, column = 0, pady = 1)
bottom_frame.grid(row = 3, column = 0, pady = 3)
#labels, textboxes, buttons
#top_frame content
Notes_label = Label(top_frame, text = "Notes", fg = "black", font = 1, padx = 141)
Notes_label.grid(row = 0 , column = 0)
save_button = Button(top_frame, text = "save")
#padx increases distance between buttons
#button_frame content
#bold button
#the ideal is that if u press ctrl + b, the bold_button is pressed by itself
#rn, it's gonna be a highlight technique
bold_button = Button(button_frame, text = "B", padx = 4, pady = 2, command = bold_it)
bold_button.grid(row = 0, column = 0)
#italicsize button
italics_button = Button(button_frame, text = "I", padx = 4, pady = 2, command = italics_it)
italics_button.grid(row = 0, column = 2, padx = 15)
#text_box frame button
text_box = Text(text_frame, width = 45, height = 27)
text_box.grid(row = 0, column = 0)
#text_box frame content
main_scrollbar = ttk.Scrollbar(text_frame, orient = "vertical", command = text_box.yview)
main_scrollbar.grid(row = 0, column = 1)
text_box["yscrollcommand"] = main_scrollbar.set
clear_button = Button(bottom_frame, text = "clear", padx = 2, pady = 2, command = clear)
clear_button.grid(row = 0, column = 0, padx = 15, pady = 10)
save_button = Button(bottom_frame, text = "save note", padx = 2, pady = 2)
save_button.grid(row = 0, column =1, padx = 15, pady = 10)
#binding all buttons for changing colours when user hovers over it and leaves it
bold_button.bind("<Enter>", enter_button)
bold_button.bind("<Leave>", leave_button)
italics_button.bind("<Enter>", enter_button)
italics_button.bind("<Leave>", leave_button)
clear_button.bind("<Enter>", enter_button)
clear_button.bind("<Leave>", leave_button)
save_button.bind("<Enter>", enter_button)
save_button.bind("<Leave>", leave_button)
# main program loop
root.mainloop()
here's an image of the problem image of problem
I would also be very grateful if one could explain the concept of scrollbar.set and like yview and why they are both needed for the scrollbar to work. Tutorials and videos don't seem to explain the concept, but just implement it
In line 145. You're missing sticky
main_scrollbar.grid(row = 0, column = 1, sticky=NS)
Output:
I have several larger tkinter / python program which I would like to incorporate into one program which would clear a frame when another program is called; each program currently being inside a function (I probably should use classes eventually when I understand them) and each of these function being displayed on a form being cleared of widgets from the previous if any do exist.
The code below is just a small trial for me to understand how to do this, but it's not working.
When I invoke the widget.destroy() function, it removes the frame (DisplayFrame) and does not clear the widgets inside it and hence not displaying the new widgets.
here is the current trial code:
#!/usr/bin/env python3
import tkinter as tk
from tkinter import *
from tkinter import ttk
#import pandas as pd
import MultiTempsP3
import datetime, time
from tkinter import messagebox
import sqlite3
from tkinter import colorchooser
from configparser import ConfigParser
import os
import csv
if os.environ.get('DISPLAY','') == "":
print('no display found.Using :0.0')
os.environ.__setitem__('DISPLAY',':0.0')
root = tk.Tk()
root.title("Kombucha Program")
root.geometry("1400x800")
root.minsize(width=900, height=600)
#root.maxsize(width=1400, height = 900)
root.grid_rowconfigure(3, weight=1)
root.grid_columnconfigure(2, weight=1)
root.configure( bg = '#000080' )
DisplayFrame = tk.Frame(root, width=1200, height = 630, bg = 'yellow') #0059b3')
DisplayFrame.grid(column=0,row=1, sticky = N, in_ = root)
rightFrame = tk.Frame(root, width = 120, height = 390, bg = 'white') # #000080
rightFrame.grid(column = 1, row = 0, pady = 10, padx = 10)
lblFrame = tk.Frame(root, height = 70, width = 670, bg = 'black')
lblFrame.grid(column = 0, row = 0, sticky =N, in_ = root)
##'W' stands for West = WrightFrmae (west fframe on the right of screen
#WrightFrame = tk.Frame(rightFrame, width = 70, height = 300, bg = 'green') # #000080
#WrightFrame.grid(column = 0, row = 1)
WidgetFrame = tk.Frame(root, height = 300, width = 120, bg = 'red') # #000080
WidgetFrame.grid(column=0,row=2, pady = 30)
fromTemp = MultiTempsP3.temps("65cd6bd")
lblTemp = Label(rightFrame, text=fromTemp).grid(row=1,column=0,pady=0 )
#lblTemp.pack()
def clearDisplayFrame():
for widgets in DisplayFrame.winfo_children():
widgets.destroy()
###***### - This section is in the right top little frame = rightFrame
state = "yes" ## delete this row and use below state=GPIO when on an RPi
#state = GPIO.input(17)
if state:
state_17="GPIO_17 (HeatPad) is On "
else:
state_17="GPIO_17 (HeatPad) is Off "
lblHeatPad = Label(rightFrame, text=state).grid(row=3,column=0,pady=0 ) #shows as text in the window
#lblHeatPad.pack() #organizes widgets in blocks before placing them in the parent.
###***### End of rightFrame widgets
def func_quit():
root.destroy()
def openData():
clearDisplayFrame()
print("I am inside openData()")
lbltrial=tk.Label(DisplayFrame,text="trial").grid(row=3, column=2)
def func_Temps():
clearDisplayFrame()
print("I am inside func_Temps()")
#DisplayFrame = tk.Frame(root, width=1200, height = 630, bg = 'yellow') #0059b3')
#DisplayFrame.grid(column=0,row=1, sticky = N, in_ = root)
lblSomething = tk.Label(DisplayFrame, text = "Open Temps").grid(row=2,column=2)
###***### This section is top of left = lblFrame
exitButton = tk.Button(lblFrame, text="Quit the Program", width = 12, command=root.destroy, bg= "magenta")
exitButton.grid(row = 0, column = 0, columnspan = 1, pady = 5, padx = 5)
dataButton = Button(lblFrame, text="Open Dates Window", command=openData).grid(row=0, column=1) ## the open refers to the above function
tempsButton= Button(lblFrame, text="Open Temps Info", command=func_Temps).grid(row=0, column=2)
###***### End of top left widget in lblFrame
mainloop()
As an answer, here is an approach that uses 2 frame and switches between them in the click of the switch. This is the way usually switching between frame is implemented in procedural programming, AFAIK:
from tkinter import *
root = Tk()
def change(frame):
frame.tkraise() # Raising the passed frame
window1 = Frame(root)
window2 = Frame(root)
window1.grid(row=0,column=0) # Grid in the same location so one will cover/hide the other
window2.grid(row=0,column=0)
# Contents inside your frame...
Label(window1,text='This is page 1',font=(0,21)).pack()
Label(window2,text='This is page 2',font=(0,21)).pack()
# Buttons to switch between frame by passing the frame as an argument
Button(root,text='Page 1',command=lambda: change(window1)).grid(row=1,column=0,stick='w')
Button(root,text='Page 2',command=lambda: change(window2)).grid(row=1,column=0,stick='e')
root.mainloop()
So instead of destroying all the items inside your frame, you should just raise the other frame, as destroyed widgets cannot be brought back.
Is it possible to add an image to a combobox in tkinter?
I tried the following, but I already expected it to not work.
import tkinter as tk
from tkinter import ttk
from tkinter import *
# Creating tkinter window
window = tk.Tk()
window.title('Combobox')
window.geometry('1440x900')
main_canvas = Canvas(
window,
bg = "#FFFFFF",
height = 900,
width = 1440,
bd = 0,
highlightthickness = 0,
relief = "ridge"
)
main_canvas.place(x = 0, y = 0)
main_button_image_1 = PhotoImage(
file="button_1.png")
n = tk.StringVar()
monthchoosen = ttk.Combobox(window, width = 27, textvariable = n, image=main_button_image_1)
# Adding combobox drop down list
monthchoosen['values'] = ('new')
monthchoosen.place(
x=10.0,
y=826.0,
width=55.0,
height=55.0
)
monthchoosen.current(0)
window.mainloop()
Basically I want to make the combobox look a bit better and the easiest way would be to design an image and add it to the combobox.
PS:
I just copied real quick a few thinks, so please don't mind the variable names.
No, the ttk.Combobox widget does not support images.
I want to read an input from entry widget and display it in canvas on clicking the button.I created a canvas and I tried this code
entryval= Tkinter.Entry(framename)
entryval.pack()
button = Tkinter.Button(entryframe, text ="Enter",command=print)
button.pack()
def print()
print entryval.get
But result is displaying only in terminal.not in canvas.
Please help.Thanks in advance
Here's a quick demo I made to help printing text to canvas:
from Tkinter import *
window = Tk()
def printVal():
canvas = Canvas(window, width = 100, height = 100)
canvas.grid(row = 0, column = 0, columnspan = 2)
string = entryval.get()
canvas.create_text(50,50, text = string)
entryval = Entry(window)
entryval.grid(row = 1, column = 0)
button = Button(window, text = "Print", command = printVal)
button.grid(row = 1, column = 1)
window.mainloop()