i'm new to tkinter and I was trying to make a GUI where there was an image at the top with an area of 4 buttons underneath that image which would be a method of selecting answers. However with the code I have so far the buttons that I create just seem to stay in the top left corner and will not move under the image at all, does anybody know a solution to this please?
import Tkinter as tk
from Tkinter import *
from Tkinter import PhotoImage
root = Tk()
class Class1(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.master = master
self.question1_UI()
def question1_UI(self):
self.master.title("GUI")
gif1 = PhotoImage(file = 'Image.gif')
label1 = Label(image=gif1)
label1.image = gif1
label1.grid(row=1, column = 0, columnspan = 2, sticky=NW)
questionAButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionAButton.grid(row = 2, column = 1, sticky = S)
questionBButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionBButton.grid(row = 2, column = 2, sticky = S)
questionCButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionCButton.grid(row = 3, column = 3, sticky = S)
questionDButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionDButton.grid(row = 3, column = 4, sticky = S)
def main():
ex = Class1(root)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(),
root.winfo_screenheight()))
root.mainloop()
if __name__ == '__main__':
main()
You are not using self as the parent of label1. Besides, the grid manager starts at row 0:
def question1_UI(self):
# ...
label1 = Label(self, image=gif1)
label1.image = gif1
label1.grid(row = 0, column = 0, columnspan = 2, sticky=NW)
questionAButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionAButton.grid(row = 1, column = 0, sticky = S)
questionBButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionBButton.grid(row = 1, column = 1, sticky = S)
questionCButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionCButton.grid(row = 2, column = 0, sticky = S)
questionDButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionDButton.grid(row = 2, column = 1, sticky = S)
Related
i'm french so sorry for the translation and i'm beginner on python.
I have a items display problem in canvas. Refering to the following code, the script doesn't work properly if i want generate a high number of items into my canvas.
All items are not displayed properly
2)the script is freezing a little bit
I dont no why...Someone can help me please?
from tkinter import *
from tkinter import ttk
import tkinter as tk
class Main_frame(Frame):
# Init
def __init__(self, fenetre_principale=None):
tk.Frame.__init__(self, fenetre_principale)
self.grid(sticky = "news")
self.main_frame = tk.Frame(self, bg = "blue")
self.main_frame.grid(sticky = "news")
self.frame_canvas = tk.Frame(self.main_frame, bg = "yellow")
self.frame_canvas.grid(row = 2, column = 0, pady = 5, sticky="nw")
self.frame_canvas.grid_rowconfigure(0, weight=1)
self.frame_canvas.grid_columnconfigure(0, weight=1)
self.frame_canvas.grid_propagate(False)
self.canvas = tk.Canvas(self.frame_canvas, bg = "yellow")
self.canvas.grid(row = 0, column = 0, sticky='news')
self.vbar = tk.Scrollbar(self.frame_canvas, orient = "vertical", command = self.canvas.yview)
self.vbar.grid(row = 0, column = 1, sticky = "ns")
self.canvas.configure(yscrollcommand= self.vbar.set)
self.frame_buttons = tk.Frame(self.canvas, bg = "grey")
self.canvas.create_window((0, 0), window=self.frame_buttons, anchor="nw")
self.ajout()
def ajout(self):
self.bu_aj = tk.Button(self.main_frame, text = "Ajout", command = self.update_).grid(row = 0, column= 0, pady = 5, sticky="nw")
def update_(self):
for k in range(2000):
self.bu = tk.Button(self.frame_buttons, text = f"Test{k}")
self.bu.grid(row = k, column= 0, sticky="news")
self.frame_buttons.update_idletasks()
first5columns_width = sum([self.bu.winfo_width() for j in range(0, 15)])
first5rows_height = sum([self.bu.winfo_height() for i in range(0, 5)])
self.frame_canvas.config(width=first5columns_width + self.vbar.winfo_width(),height=first5rows_height)
self.canvas.config(scrollregion=self.canvas.bbox("all"))
print("ok")
if __name__ == "__main__":
root = tk.Tk()
root.grid_rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.title("Title")
interface = Main_frame(fenetre_principale=root)
interface.mainloop()
I'm trying to make a text editor with Python 3.4.2 and tkinter/ttk. When my file is loaded in, the text is inserted into the Text widget from the bottom of the file up, making the file text "backwards". The last line of the file is inserted first, and the first line last. Anything will help. Thank you.
from tkinter import *
from tkinter import ttk
import os
class Main(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.body = Text(self, width=50, height=15)
self.body.grid(row = 0, column = 0, sticky = W)
self.save_label = Label(self, text="File to Save to:")
self.save_label.grid(row = 2, column = 0, sticky = W)
self.save_entry = ttk.Entry(self)
self.save_entry.grid(row = 3, column = 0, sticky = W)
self.save_button = ttk.Button(self, text="Save File", command=self.save)
self.save_button.grid(row = 4, column = 0, sticky = W)
self.read_label = Label(self, text="File to Read:")
self.read_label.grid(row = 2, column = 1, sticky = W)
self.read_entry = ttk.Entry(self)
self.read_entry.grid(row = 3, column = 1, sticky = W)
self.read_button = ttk.Button(self, text="Read File", command=self.read)
self.read_button.grid(row = 4, column = 1, sticky = W)
scrollbar = ttk.Scrollbar(root, orient=VERTICAL, command=self.body.yview)
scrollbar.grid(row = 0, column = int(1), sticky = 'ns')
self.body.config(yscrollcommand=scrollbar.set)
self.quit_button = ttk.Button(self, text="Quit", command=self.close)
self.quit_button.grid(row = 5, column = 0, sticky = W)
def close(self):
root.destroy()
quit()
def save(self):
body = self.body.get('0.0', 'end-1c')
file = self.save_entry.get()
file = open(file, "w")
file.write(body)
file.close()
def read(self):
self.body.delete("0.0", END)
file = self.read_entry.get()
file = open(file, 'r')
file_data = file.readlines()
for i in file_data:
self.body.insert('1.0', str(i))
file.close()
root = Tk()
root.title("NotePad")
root.geometry("500x600")
root.attributes("-fullscreen", True)
app = Main(root)
root.mainloop()
You are telling tkinter to place each line at "1.0". If you want each line to be added to the end, use "end"
for i in file_data:
self.body.insert('end', str(i))
By the way, the first character of a text widget is at "1.0", not "0.0". While "0.0" will work as quirk of how tkinter is implemented, the correct index is "1.0".
For example, use this:
body = self.body.get('1.0', 'end-1c')
... rather than this:
body = self.body.get('0.0', 'end-1c')
I'm new to Python and just started venturing into doing GUIs. I created a Tkinter window that is pretty basic: it has 3 Entry bars and 3 File Dialog buttons. When you chose the 3rd directory, the GUI file automatically makes a call to a separate file and receives a large text block which is then displayed in a Text box.
The whole thing works correctly, but my problem is that after receiving and inserting the text response, Tkinter stops working and doesn't allow the user to scroll down.
I read that one reason this happens is because people use both .pack( ) and .grid( ), but I'm not mixing those two functions.
Thanks in advance for any help!
Here's my GUI file
from tkinter import *
from tkinter import filedialog
from gui_GPSExtractor import *
import os
class Application(Frame):
def __init__(self, master):
""" Initialize Frame """
Frame.__init__(self, master)
self.grid( )
self.startGUI( )
""" Create Labels, Text Boxes, File Dialogs, and Buttons """
def startGUI(self):
# Label for Scan Path
self.dLabel = Label(self, text = "Scan Path")
self.dLabel.grid(row = 0, column = 0, columnspan = 2, sticky = W)
# Entry for Scan Path
self.dEntry = Entry(self, width = 60)
self.dEntry.grid(row = 1, column = 0, sticky = W)
# Button for Scan Path Directory Browse
self.dButton = Button(self, text = "Browse", command = lambda: self.browseFiles("d"))
self.dButton.grid(row = 1, column = 1, sticky = W)
# Label for CSV Path
self.cLabel = Label(self, text = "CSV Path")
self.cLabel.grid(row = 3, column = 0, columnspan = 2, sticky = W)
# Entry for CSV Path
self.cEntry = Entry(self, width = 60)
self.cEntry.grid(row = 4, column = 0, sticky = W)
# Button for CSV Path Directory Browse
self.cButton = Button(self, text = "Browse", command = lambda: self.browseFiles("c"))
self.cButton.grid(row = 4, column = 1, sticky = W)
# Label for Log Path
self.lLabel = Label(self, text = "Log Path")
self.lLabel.grid(row = 6, column = 0, columnspan = 2, sticky = W)
# Entry for Log Path
self.lEntry = Entry(self, width = 60)
self.lEntry.grid(row = 7, column = 0, sticky = W)
# Button for Log Path Directory Browse
self.lButton = Button(self, text = "Browse", command = lambda: self.browseFiles("l"))
self.lButton.grid(row = 7, column = 1, sticky = W)
# Text Box for Results
self.resultText = Text(self, width = 60, height = 30, wrap = WORD, borderwidth = 3, relief = SUNKEN)
self.resultText.grid(row = 9, column = 0, columnspan = 2, sticky = "nsew")
# Scrollbar for Text Box
self.scrollBar = Scrollbar(self, command = self.resultText.yview)
self.scrollBar.grid(row = 9, column = 2, sticky = "nsew")
self.resultText["yscrollcommand"] = self.scrollBar.set
def browseFiles(self, btnCalling):
if(btnCalling == "d"):
self.dName = filedialog.askdirectory(initialdir = "/python3-CH05")
self.dEntry.delete(0, END)
self.dEntry.insert(0, self.dName)
elif(btnCalling == "c"):
self.cName = filedialog.askdirectory(initialdir = "/python3-CH05")
self.cEntry.delete(0, END)
self.cEntry.insert(0, self.cName)
elif(btnCalling == "l"):
self.lName = filedialog.askdirectory(initialdir = "/python3-CH05")
self.lEntry.delete(0, END)
self.lEntry.insert(0, self.lName)
output = extractGPS(self.dName, self.cName, self.lName)
self.resultText.delete(0.0, END)
self.resultText.insert(0.0, output)
# Start the GUI
root = Tk( )
root.title("Python gpsExtractor")
root.geometry("650x650")
app = Application(root)
root.mainloop( )
i'm trying to make this simple gui using grid layout where i have in one row a label an entry and a button, but for some reason the first button always takes the rowspan equal to the number of rows in previous column, even if i try to force it to have rowspan 1 it has no effect which makes me really confused.
import tkinter as tk
class MainApplication(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.grid()
#LABELS
self.l1 = tk.Label(self, text = "Source")
self.l1.grid(column = 0, row = 0, sticky = "E")
self.l2 = tk.Label(self, text = "Text files destination")
self.l2.grid(column = 0, row = 1, sticky = "E")
self.l3 = tk.Label(self, text = "Image files destination")
self.l3.grid(column = 0, row = 2, sticky = "E")
#ENTRIES
self.e1 = tk.Entry(self)
self.e1.grid(column = 1, row = 0)
self.e2 = tk.Entry(self)
self.e2.grid(column = 1, row = 1)
self.e3 = tk.Entry(self)
self.e3.grid(column = 1, row = 2)
#BUTTONS
self.b3 = tk.Button(text = "Select dir", command = self.cb2)
self.b3.grid(column = 2, row = 0)
self.b4 = tk.Button(text = "Select dir", command = self.cb2)
self.b4.grid(column = 2, row = 1)
self.b5 = tk.Button(text = "Select dir", command = self.cb2)
self.b5.grid(column = 2, row = 2)
if __name__ == "__main__":
root = tk.Tk()
app = MainApplication(root)
root.mainloop()
output:
http://i.imgur.com/AdWkHwi.png
You don't specify a parent for the buttons, so their parent is the root window. The labels and entries, on the other hand, have their parent attribute set to the frame. What happens is that in the root, the frame is in row zero, and the first button is in row zero, and the height of the row is determined by the height of the frame.
The solution is to make the parent of the buttons be the frame.
self.b3 = tk.Button(self, ...)
self.b4 = tk.Button(self, ...)
self.b5 = tk.Button(self, ...)
I'm working on my final project for my computing I class.
The problem that I am having is:
When I click on the new entry button, hit the back button and click on the new entry button once again it does not work.
If you guys could tell me why that is?
The command on the button seems to be only working once. Thanks for your help.
Code:
from tkinter import *
import tkinter.filedialog
class App(Tk):
def __init__(self):
Tk.__init__(self)
self.title("Entry Sheet")
self.font = ("Helvetica","13")
self.header_font = ("Helvetica","18")
self.exercise_font = ("Helvetica","13","bold")
self.delete = 'a'
self.new_user()
def new_user(self):
if self.delete == 'b':
self.delete = 'c'
self.hide()
self.delete = 'b'
self.new_entry = Button(self, text = 'New Entry', command = self.entry, width = 15)
self.new_entry.grid(row = 1, column = 0, columnspan = 3, padx = 10, pady = 5)
self.look_entry = Button(self, text = 'See Entries', command = self.see_entries, width = 15)
self.look_entry.grid(row = 2, column =0, columnspan = 3, padx = 10, pady = 5)
def entry(self):
print(1)
self.delete = 'b'
self.hide()
self.entry = Label(self, text = 'New Entry', font = self.header_font)
self.entry.grid(row = 0, column = 0, columnspan = 2)
self.numberlbl = Label(self, text = 'Please choose a muscle?', font = self.font)
self.numberlbl.grid(row = 1, column= 0, columnspan = 2, sticky = 'w' )
self.muscle_chosen = IntVar()
self.chest = Radiobutton(self, text = "chest", variable = self.muscle_chosen, value = 1, font = self.font)
self.bicep = Radiobutton(self, text = "bicep", variable = self.muscle_chosen, value = 2, font = self.font)
self.chest.grid(row = 2, column = 0)
self.bicep.grid(row = 2, column = 1)
self.exerciseslbl = Label(self, text = 'Please enter the number of exercises: ', font = self.font)
self.exerciseslbl.grid(row = 3, column = 0, columnspan = 3)
self.exercises_spinbox = Spinbox(self, from_= 1, to_= 50, width = 5, font = self.font)
self.exercises_spinbox.grid(row = 4, column = 0)
self.back_button = Button(self, text = 'Back', command = self.new_user, width = 10)
self.back_button.grid(row =5, column=0, pady =10)
def see_entries(self):
print("Goes through")
def hide(self):
if self.delete == 'b':
self.new_entry.grid_remove()
self.look_entry.grid_remove()
elif self.delete == 'c':
self.entry.grid_remove()
self.numberlbl.grid_remove()
self.chest.grid_remove()
self.bicep.grid_remove()
self.exerciseslbl.grid_remove()
self.exercises_spinbox.grid_remove()
self.back_button.grid_remove()
def main():
app = App()
app.mainloop()
if __name__=="__main__":
main()
In your entry function you overwrite self.entry, which is the name of the function, with a reference to a Label. When the button then calls self.entry it isn't function.
Simply call the Label something else.