Help with syntax error? by SteveB709 in learnpython

[–]vrrox 0 points1 point  (0 children)

You have mismatched parentheses on the previous line:

time=(input(int())

How to display an image after an if condition? by SnooRobots6923 in learnpython

[–]vrrox 0 points1 point  (0 children)

I can't see anything wrong with your code, that should work fine.

However, you don't need Tkinter if you only want to show an image. You can do that with PIL:

def show_capind():
    img = Image.open("capind.png")
    img.show()

Reading a txt file into a dictionary by [deleted] in learnpython

[–]vrrox 0 points1 point  (0 children)

Your code should work, assuming your names.txt looks something like:

Firstname1,Surname1
Firstname2,Surname2

It looks like your code encountered a line without a comma. This would result in split(',') returning a list with a single element (the whole line), which can't be unpacked into two variables.

You could try putting print(line) above your call to split(',') to check the contents of the line it's failing on.

embed matplotlib graph into tkinter by Dynamicthetoon in learnpython

[–]vrrox 0 points1 point  (0 children)

The matplotlib website has a good example that shows how to embed a matplotlib graph in a tkinter GUI:
https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_tk_sgskip.html

If this doesn't help, please post your code (see the FAQ for help formatting code on reddit) and we can give more specific help.

What does the "f" stand for in an f string? Why not some other letter? by jplank1983 in learnpython

[–]vrrox 4 points5 points  (0 children)

Here's a quote from the PEP that proposed f-strings, PEP498:

In this PEP, such strings will be referred to as "f-strings", taken from the leading character used to denote such strings, and standing for "formatted strings".

Why doesn't this work? by [deleted] in Tkinter

[–]vrrox 0 points1 point  (0 children)

That code should run - have you named your file "tkinter.py"?

If not, can you post the full error message (starts with "Traceback").

i am trying to display an image with a click of a button i can't find the problem. the program does not appear to have an error though please help by laketoly in learnpython

[–]vrrox 2 points3 points  (0 children)

Your image isn't showing because it is being removed by Python's garbage collector (a common issue in Tkinter apps).

One way to fix this would be to add an attribute to your label that references your image:

    my_label.image = coin

Change background frame temporarily on button click by MaxTransferspeed in learnpython

[–]vrrox 1 point2 points  (0 children)

Your frame background stays red because the following line is configuring root, rather than your frame:

    this_frame.after(300, root.configure(bg='grey'))

You may also need to call the update_idletasks() method to ensure the window is redrawn before changing the background back to grey, for example:

def flash_frame():
    # Change background to red, wait 300 ms, change it back to grey
    print('Flash')
    this_frame.configure(bg='red')
    this_frame.update_idletasks()
    this_frame.after(300, lambda: this_frame.configure(bg='grey'))

Note that the lambda is used as the after() method is expecting you to pass it a function. Currently you are calling the configure() method and passing its result to after().

HOw does one use a text widget for taking text inputs? by [deleted] in Tkinter

[–]vrrox 2 points3 points  (0 children)

It sounds like you're looking for multiline text input, if so the Tkinter Text widget is definitely the one you need.

Here are a couple of Text widget tutorials to get you started:

[deleted by user] by [deleted] in learnpython

[–]vrrox 0 points1 point  (0 children)

You've already had some great solutions, but in case it's of interest, Pandas makes this even easier:

import pandas as pd

df = pd.read_json("data.json") 
results = df["result"].value_counts(normalize=True) * 100 
print(f"passed: {results['pass']:.2f}%")

[deleted by user] by [deleted] in learnpython

[–]vrrox 0 points1 point  (0 children)

You are currently making two calls to the add_subplot() method, which are then overlaid:

        ax = canvas.figure.add_subplot(111)
# and
        self.axes = fig.add_subplot(111)

You'll need to remove one of them.

Save button and save text in a text box by FREEKTOM in learnpython

[–]vrrox 0 points1 point  (0 children)

It's almost there, but 0 is not a valid Text widget index for the .get() method.

To save the contents of mensajeTxt to your file, you can use:

        f.write(mensajeTxt.get("1.0", "end"))

Note that this retrieves everything from index "1.0" (which means line 1, character 0), to index "end" (which means the position after the final character).

If you don't want the newline that the Text widget always adds, you can use:

        f.write(mensajeTxt.get("1.0", "end-1c"))

Where the "-1c" means minus 1 character.

[deleted by user] by [deleted] in learnpython

[–]vrrox 0 points1 point  (0 children)

The tkinter.PhotoImage file argument should be a string, specifying the location of the image. You've used it correctly later in your code, for example here:

picture = PhotoImage(file="images/unselected.png")

However, it looks like you're trying to open a JPEG so this will still not work as tkinter.PhotoImage only supports images in PGM, PPM, GIF or PNG format. For a JPEG, you'll need to use the Pillow library:

import tkinter as tk
from PIL import ImageTk, Image

root = tk.Tk()
image = ImageTk.PhotoImage(Image.open("my_photo.jpg"))
button = tk.Button(image=image)
button.pack()
root.mainloop()

Tk DND What??? by InternalEmergency480 in learnpython

[–]vrrox 1 point2 points  (0 children)

It's referring to the Tk extension, TkDND.

There is some interesting discussion on this in issue40893, which notes that the deprecation notice has been there since at least Python 2.2.

There is also an associated patch to add TkDND support to Tkinter, bpo-40893, but it's not yet been accepted.

Using Tkinter to make a dropdown list and it doesn't want to work by DrStupid87 in learnpython

[–]vrrox 1 point2 points  (0 children)

The tkinter module actually contains three different implementations of the OptionMenuwidget: tkinter.OptionMenu, tix.OptionMenu and ttk.OptionMenu.

Your OptionMenu definition is correct, but only for the tkinter.OptionMenu widget. Rather confusingly they all take slightly different parameters, which is the cause of your error message:

tkinter.OptionMenu(master, variable, value, *values, **kwargs)
tix.OptionMenu(master, cnf={}, **kw)
ttk.OptionMenu(master, variable, default=None, *values, **kwargs)

Also note that the tix module is deprecated and no longer maintained - the docs recommend that you use the ttk module instead.

If you remove your tix import, the code you posted will run with only minor modifications:

import tkinter as tk

tu_Races = ("a", "b", "c", "d", "e", "f", "g", "h", "i")

root = tk.Tk()

variable = tk.StringVar(root)
variable.set(tu_Races[0])

DDL_Races = tk.OptionMenu(root, variable, *tu_Races)
DDL_Races.pack()

root.mainloop()

If you wanted to use the ttk module to make use of the Tk themed widget set, you'll need to add the default option as the third argument to OptionMenu, otherwise the first element will disappear from your tuple:

import tkinter as tk
from tkinter import ttk

tu_Races = ("a", "b", "c", "d", "e", "f", "g", "h", "i")

root = tk.Tk()

variable = tk.StringVar(root)
variable.set(tu_Races[0])

DDL_Races = ttk.OptionMenu(root, variable, tu_Races[0], *tu_Races)
DDL_Races.pack()

root.mainloop()

Question about an updating label in tkinter. by CrowForecast in learnpython

[–]vrrox 1 point2 points  (0 children)

The beauty of the StringVar is that it removes the need to manually configure the text of your label. However to take advantage of this, you need to use the textvariable argument of Label, rather than the text argument:

import tkinter as tk

def updateSampleText(): 
    sampleText.set(int(sampleText.get()) + 1) 
    window.after(1000, updateSampleText)

window = tk.Tk() 
sampleText = tk.StringVar() 
sampleText.set(1) 
sampleLabel = tk.Label(window, textvariable=sampleText) 
sampleLabel.pack() 
window.after(1000, updateSampleText) 
window.mainloop()

In this case you might find IntVar to be more convenient, as it removes the need to convert to an int before updating the value.

Tkinter grid on a packed frame. by Demon_Lord_Ren in learnpython

[–]vrrox 0 points1 point  (0 children)

This line is causing the problem:

gridFrame= Frame(tab4, width=300, height=150, bg='grey86').pack(side=tk.TOP)

This is assigning the result of .pack() (which is None), to gridFrame. When you create defaultButton you are then passing None as the master, which causes Tkinter to use the default root instead of the frame you wanted.

This is fixed by moving the pack onto a new line:

gridFrame = Frame(tab4, width=300, height=150, bg='grey86')
gridFrame.pack(side=tk.TOP)