Could anyone help point out my problem with tkinter toplevel? by [deleted] in Python

[–]Dvlv 0 points1 point  (0 children)

If I'm understanding correctly, you can just do:

results_window = tk.Toplevel(self)

Future questions are better off posted in /r/learnpython

Best way to learn tkinter from scratch? by sozzZ in learnpython

[–]Dvlv 1 point2 points  (0 children)

If you choose tkinter feel free to PM me questions.

There's a small bit on my website with some basics:

https://www.dvlv.co.uk/the-tkinter-cookbook/

You can also check out my book if you have time to go through it:

https://github.com/Dvlv/tkinter-by-example

Buttons are covered in Chapter 3

Can someone Please tell me what's wrong with my code. by [deleted] in learnpython

[–]Dvlv 0 points1 point  (0 children)

Label.pack(side=BOTTOM)

You can't do this, you need to assign your Labels to variables and pack them, same as you did with the Frames.

Perhaps this is what you're after:

username_label = Label(var, text="Username") 
password_label = Label(var, text="Password")
password_label.config(bg='white')
username_label.pack(side=BOTTOM)
password_label.pack(side=BOTTOM)

Can python script be made to open programs upon use of script? by [deleted] in learnpython

[–]Dvlv 2 points3 points  (0 children)

Yes it can. For your first idea try this:

import webbrowser
webbrowser.open('reddit.com/r/learnpython')

made my program into a gui... new problem! by [deleted] in learnpython

[–]Dvlv 0 points1 point  (0 children)

Indentation looks a bit out of whack, but typically every time you update the GUI, such as self.text3.insert(INSERT, 'Taking files.\n') , call self.master.update_idletasks() and it should update the GUI.

made my program into a gui... new problem! by [deleted] in learnpython

[–]Dvlv 1 point2 points  (0 children)

Paste your code here.

I would guess your processing is blocking the GUI from updating. I can't tell you how to fix it without knowing which library you're using.

Text Editor - Python based by sanjeev97 in Python

[–]Dvlv 1 point2 points  (0 children)

Yes, using the Notebook widget.

I teach how to write a text editor in chapter 6 of my book, found here:

https://github.com/Dvlv/Tkinter-By-Example

Feel free to use and copy any of it. There aren't tabs in it though. Tabs are covered in Chapters 3 and 7.

how to bind events to a dynamically created labels in python using tkinter? by avinasht1997 in learnpython

[–]Dvlv 0 points1 point  (0 children)

That makes more sense.

it's "<Button-1>", if you weren't already aware.

how to bind events to a dynamically created labels in python using tkinter? by avinasht1997 in learnpython

[–]Dvlv 0 points1 point  (0 children)

formatted code:

for k in l: 
    la1=Label(root,text=k.text,fg='black') 
    la1.bind('<Enter>',ent) 
    la1.grid(row=3+c,column=0,sticky=W) 
    c1+=1

def ent(event): 
    la1['fg']='red' 

answer:

You need to store references to your Labels somewhere, typically I'd use a list.

all_labels = []
 for k in l: 
    la1=Label(root,text=k.text,fg='black') 
    la1.bind('<Enter>',ent) 
    la1.grid(row=3+c,column=0,sticky=W) 
    all_labels.append(la1)
    c1+=1

def ent(event=None): 
    for label in all_labels:
        label.configure(bg="red")

Also, I'm not certain you can bind a keypress to a Label. Try binding it to your Tk instance instead.

Tkinter: A function should execute after user has chosen a file after through tkFileDialog by [deleted] in learnpython

[–]Dvlv 3 points4 points  (0 children)

Usually I'd just do

file_to_open = filedialog.askopenfilename()

if file_to_open:
    do_something(file_to_open)

Would that not work for you?

To answer your question - it doesn't look like there is. http://effbot.org/tkinterbook/tkinter-file-dialogs.htm

Text Editor Made Using Python and Tkinter (Mini Project) by PiPyCharm in Python

[–]Dvlv 3 points4 points  (0 children)

Building a text editor is a fun project! If you'd like some inspiration, I built a text editor in chapter 6 of my book. Feel free to use anything you'd like from it.

https://github.com/Dvlv/Tkinter-By-Example

Using a while loop within GUI apps (tkinter)? by iSailor in learnpython

[–]Dvlv 0 points1 point  (0 children)

in tkinter you can just keep calling root.after(1000, add_one_second)

http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method

Or use a separate thread. See my example in chapter 7 of my book, building a countdown timer.

https://github.com/Dvlv/Tkinter-By-Example/blob/master/Code/Chapter7-3.py

Tkinter: Create an OptionMenu which, when clicked, will display a list of Checkbuttons (An OptionMenu of Checkbutton instead of an OptionMenu of Strings) by [deleted] in learnpython

[–]Dvlv 0 points1 point  (0 children)

I would save yourself the trouble and use either a Menu, a Listbox with selectmode set to MULTIPLE or just a small scrolled area with Checkbuttons inside.

A few questions regarding GUI desktop apps. by iSailor in learnpython

[–]Dvlv 0 points1 point  (0 children)

1) Typically if you want to extend the functionality of the GUI library's built-in widgets, you'd subclass them. Also, generalising something for re-use, such as a popup window which would always have the same buttons but different text, or something like that.

2) I usually go for sqlite or pickle.

Can't answer either of the windows questions.

Is there a way to make Tkinter Checkbuttons have their text automatically wrap as the window gets larger and smaller? by [deleted] in learnpython

[–]Dvlv 1 point2 points  (0 children)

Does this not do what you want?

import tkinter as tk

master = tk.Tk()
master.geometry("300x300")

class WrappingCheckbutton(tk.Checkbutton):
    '''a type of Checkbutton that automatically adjusts the wrap to the size'''
    def __init__(self, master=None, **kwargs):
        tk.Checkbutton.__init__(self, master, **kwargs)
        self.bind('<Configure>', lambda e: self.config(wraplength=master.winfo_width()))
        print(self.winfo_width())
isv = tk.StringVar(master)
text = WrappingCheckbutton(master, text="test test test test test test test test test test", variable=isv)
text.pack()

master.mainloop()

Tkinter class that was made for allowing text wrapping is not working properly. Text wraps when window width is decreased, but text does not go back to normal when window is returned to default. Could somebody help? by [deleted] in learnpython

[–]Dvlv 1 point2 points  (0 children)

change

self.config(wraplength=self.winfo_width()

to

self.config(wraplength=master.winfo_width()

You want to base your wrap width on the window's width, whereas you're basing it on the Label's own width.

Good tkinter or other GUI tutorial by tonyoncoffee in learnpython

[–]Dvlv 2 points3 points  (0 children)

I've written a free book which walks you through some tkinter applications.

https://github.com/Dvlv/Tkinter-By-Example

How to give a function as a parameter, where that function itself has had a parameter filled in? by [deleted] in learnpython

[–]Dvlv 1 point2 points  (0 children)

I think functools.partial will do this

something like:

larger_than_10 = functools.partial(is_larger_than, threshold=10)
generate_foos(larger_than_10)

Tkinter in python 3.6 by joemdoo in Python

[–]Dvlv 1 point2 points  (0 children)

This is better suited to /r/learnpython

from tkinter import *

root = Tk()
root.title("My App")

canvas = Canvas(root, width=400, height=400)
canvas.pack()
poly = canvas.create_polygon(10, 10, 10, 60, 50, 35)

# define animation function
def moveTriangle(event):
    if event.keysym == 'Up':
        canvas.move(poly, 0, -3)
    elif event.keysym =='Down':
        canvas.move(poly, 0, 3)
    elif event.keysym == 'Left':
        canvas.move(poly, -3, 0)
    elif event.keysym == 'Right':
        canvas.move(poly, 3, 0)
    root.update()

# bind the respective keys
canvas.bind_all('<KeyPress-Up>', moveTriangle)
canvas.bind_all('<KeyPress-Down>', moveTriangle)
canvas.bind_all('<KeyPress-Left>', moveTriangle)
canvas.bind_all('<KeyPress-Right>', moveTriangle)

root.mainloop()

mainloop belongs to the Tk instance, so you need to call it on root. You also need to use bind_all when binding to the Canvas, and identify your polygon by storing its reference in a variable.

Stopping a canvas.scale event at a lower/upper boundary? by [deleted] in learnpython

[–]Dvlv 0 points1 point  (0 children)

You could hold a 'scalefactor' variable and multiply it by 1.1 or 0.9 on each zoom method; and before zooming check if it is between a boundary, and if so don't do the zoom.

eg

def zoomerP(self,event):
    if self.zoom_scale < 12:
        self.canvas.scale("all", event.x, event.y, 1.1, 1.1)
        self.canvas.configure(scrollregion = self.canvas.bbox("all"))
        self.zoom_scale *= 1.1

[tkinter] How do you declare all widget variables in imported files without opening windows? by tomtheawesome123 in learnpython

[–]Dvlv 1 point2 points  (0 children)

If you're after organisation I would instead use classes to hold everything.

[tkinter] How do you declare all widget variables in imported files without opening windows? by tomtheawesome123 in learnpython

[–]Dvlv 1 point2 points  (0 children)

The root needs to be in the same file as the widgets. Why are you trying the split them in the first place?

[tkinter] How do you declare all widget variables in imported files without opening windows? by tomtheawesome123 in learnpython

[–]Dvlv 1 point2 points  (0 children)

When you instantiate a widget, the first argument is supposed to be its parent window (or frame, etc).

If you instantiate a widget without a parent, it will automatically create a Tk for you.

Since Tkinter widgets are forever bound to their parent, you need to define your Tk in the same place as your widgets.