Why doesn't a line appear on my Tkinter canvas? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I am trying to draw a simple line in Tkinter using a Canvas. I have this code so far:
from tkinter import *
from tkinter.ttk import *
rt = Tk()
rt.geometry('500x500')
head = Canvas(rt, width=500, height=100)
head.create_line(1, 99, 499, 99)
rt.mainloop()
When I run the program I only get a plain white screen. Why doesn't the line appear?

As #jasonharper mentioned, you aren't actually adding the canvas widget to the UI with a geometry manager like pack(), place() or grid(). Try...
import tkinter as tk # avoid star imports if you can!
from tkinter import ttk
rt = tk.Tk()
rt.geometry('500x500')
head = tk.Canvas(rt, width=500, height=100)
head.pack(expand=True, fill=tk.BOTH) # <- add this
head.create_line(1, 99, 499, 99)
rt.mainloop()

Related

How can I create a text file that saves an entry from tkinter [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is my current db.py file, it contains the tkinter code for creating the GUI:
import tkinter
import db
app = Tk()
app.geometry("450x300")
app.mainloop()
You can use a Entry widget with a linked variable to get the input from user. The content can then be retrieved from the variable and written to a file using file objects.
I like to use themed-tkinter(ttk). If you just starting with creating GUI, I suggest you read more about themed-tkinter here.
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
# StringVar has all the logic to store and update string values
content = tk.StringVar()
def callback():
# This function is invoked when button `submit` is clicked
with open('content.txt', 'w') as file:
file.write(content.get())
entry = ttk.Entry(root, textvariable=content).grid()
submit = ttk.Button(root, text='submit', command=callback).grid()
root.mainloop()
Edit: I worded my answer wrongly for which I appologize. Tkinter by itself is indeed robust and powerful. I found it more easier to use ttk in many cases and had a softcorner towards it.

How to solve "Name error" in Python tkinter programme? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
from tkinter import *
root = Tk()
mylabel = label(root, text = "What's up?")
mylabel.pack()
root.mainloop()
This is the code I used in Visual code studio. And it shows me an error:
NameError: name 'label' is not defined
What do I need to change to make this work?
pls change your code like this
mylabel = Label(root,text = '')
you should use Lable instead label

How to play a sound sample on every tkinter.text input? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I need to play a typewriter key sample on every tkinter.text input.
I came across the Playsound module but I don't know how to listen to the inputs.
You would use a bind and setup a function to play the sound when the bind is triggered.
import tkinter as tk
def key(event):
print("pressed", repr(event.char))
# Play sound here
root = tk.Tk()
text = tk.Text(root)
text.pack()
text.bind('<Key>', key)
root.mainloop()
Thanks, I've come up with a very similar solution tho its really really laggy. Im basically writing a simple typewriter emulator so the key sound is reproduced for each typed letter.
import tkinter as tk
from PIL import Image, ImageTk
from playsound import playsound
def key(event):
key = event.char
playsound("C:/Users/Isma/key1.mp3")
win = tk.Tk()
frame = tk.Frame(win, width=300, height=400)
frame.grid(row=1, column=0)
text = tk.Text(frame)
text.grid(row=0,column=0)
text.bind('<Key>',lambda a : key(a))
image = Image.open("C:/Users/Isma/swintec1.jpg")
photo = ImageTk.PhotoImage(image)
label = tk.Label(frame,image=photo)
label.image = photo
label.grid(row=3,column=0)
win.mainloop()

Can you tell me why i get the Error Message invalid syntax on my command [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
from random import*
from tkinter import*
players=['wq','qwe','qwe']
players_to_random=[]
window = Tk()
window.title("Game is ON")
w= Label(window, bg="yellow")
def onclick():
players_to_random.append(text)
print(players_to_random)
def showButtons():
for i in players:
btn = Button(window, text=i command=onclick)
btn.pack(side=LEFT)
showButtons()
I get the Error command invalid syntax but as far as i know it is possible to use command as a parameter for Buttons.So why does it show this Error
Check the indentation and syntaxis in the whole shouButtons function.
The block of code in the for loop must be indented. Also there is a comma missing between Button parameters text and command.
def showButtons():
for i in players:
btn = Button(window, text=i, command=onclick)
btn.pack(side=LEFT)

Why isn't text showing in Tkinter? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I can't understand why the text isn't showing. I tried to reformat the program but it isn't showing the text.
import tkinter as tk
import random
Test = "test"
root = tk.Tk()
label = tk.Label(root, textvariable = Test, width = 30)
label.pack()
root.mainloop()
effbot is the recommend documentation for the tkinterlibrary.
The Label.textvariable attribute should point to a tkinter.StringVar() object:
>>> import tkinter as tk
>>> root = tk.Tk()
>>> test = StringVar()
>>> test.set('Hello')
>>> pinnarKvar = tk.Label(root, textvariable = test, width = 30)
>>> pinnarKvar.pack()
HereI am using IDLE so I don't use root.mainloop() since it dynamically updates.
To change what you see, after you call pinnarKvar.pack() call text.set() to update.

Categories

Resources