This question already has an answer here:
Python import own module - name not defined
(1 answer)
Closed 4 years ago.
I am having trouble with some code in python 3.7 involving Tkinter. I am using an online tutorial, specifically this one. I am stuck on how the END within the text widget is used. Here is my code:
import tkinter
GUI = tkinter.Tk()
GUI.title("Dungeon Crawler")
GUI.wm_iconbitmap('dragon.ico')
GUI.configure(background="#000000")
lbl = tkinter.Label(GUI, text="Input", fg="#fff", bg="#000000")
scribe = tkinter.Entry(GUI)
txt = tkinter.Text(GUI)
lbl.grid(row=0, column=0)
scribe.grid(row=1, column=0)
txt.grid(row=0, column=1)
txt.insert(END, "Just a text Widget\nin twolines\n")
GUI.mainloop()
Since you have used import tkinter you need to do the following
txt.insert(tkinter.END, "Just a text Widget\nin twolines\n")
Rather than having to write tkinter everywhere you could change your code to import tkinter but rename it as tk.
import tkinter as tk
....
GUI = tk.Tk()
....
txt = tk.Text(GUI)
txt.insert(tk.END, "Just a text Widget\nin twolines\n")
Related
This question already has answers here:
How to handle a click button event in python module tkinter
(2 answers)
Closed 2 years ago.
Here is my code!
///
from tkinter import *
import os
import pygame
os.system(‘clear’)
#Window Setup
root = Tk()
root.title(‘cottontail’)
root.geometry(‘800x600’)
frame = Frame(root)
title_screen = Label(root, text = “Choose your rabbits name!”)
title_screen.pack()
name = Text(root, width=10 , height=3)
name.pack()
confirm_name = Button(root, text= “Conirm?”, width = 5, height=3)
root.mainloop()
///
My objective is to take the input the user puts in the text box to make a label in a pygame window with that name. I figured that a button would be an easy way to confirm the name and open the pygame screen. If this makes any sense to you it would really be appreciated if you could help me. Hope you have a good night!
Thanks in advance!
I'm not familiar with tkinter as I'm a PyQt dev, but I can help with your question as the logic behind both of them are same.
You can make a function that is called by the button whenever its clicked, and then, inside that function definition, do whatever you want.
Here's the website that explains this in detail -
https://www.delftstack.com/howto/python-tkinter/how-to-get-the-input-from-tkinter-text-box/
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Tkinter PIL image not displaying inside of a function
(1 answer)
Closed 2 years ago.
So I'm experimenting With Tkinter message boxes And I'm trying to insert an image in my toplevel window>
from tkinter import*
from tkinter import messagebox
root = Tk()
root.title('Hi')
root.iconbitmap('C:/Users/davids/Downloads/i2.ico')
e = Entry(root,borderwidth=8,width=35)
top = Toplevel()
def popup():
mg = messagebox.askyesno("Warning", "Click yes/No.")
if mg == True:
top = Toplevel()
top.geometry('1200x1200')
b= Label(top, text="hi")
b.pack()
image = PhotoImage(file="path")
Label(root, image=image).pack()
Button(root,text="Ask question", command=popup).pack()
root.mainloop()
However when I run this code nothing shows. There isn't an error so I can't see what I'm doing wrong. Only the text is displayed. I've tried adding root. but that still doesn't fix it. Please note that I do know that it is easier to use PIL however my new operating system won't install it for some reason so I'm trying to find a way without Pillow. Any idea what is happening?
I am new to tkinter and have been using:
from tkinter import *
but have read this is bad practice.
I rewrote a very small bit of code to start using the following:
import tkinter as tk
However when I run the rest of the code. I get the error:
label.place(relx=0.4, rely=0.35, anchor=CENTER)
NameError: name 'CENTER' is not defined
root = tk.Tk()
label = tk.Label(root, text="I am a label widget")
label.place(relx=0.4, rely=0.35, anchor=CENTER)
button = tk.Button(root, text="I am a button")
label.pack()
button.pack()
root.mainloop()
Is this a namespace issue? How can I solve the problem?
* gets all the sub packages. Using import tkinter as tk just changes the name of the package from tkinter to tk.
You have not told your script CENTER is part of tkinter. (you did this automatically when you used *) but now you must do by explicitly telling CENTER is part of tkinter:
tk.CENTER
CENTER is a variable(actually they usually are referred to as constants) of tkinter module which equals to 'center'. So simply replace the line with:
label.place(..., anchor='center')
This question already has answers here:
Easiest way to develop simple GUI in Python [closed]
(7 answers)
Closed 6 years ago.
I am learning to program with Python 3.6, and would like to ask for help on building a window. It would be greatly appreciated if someone would please show me the basics, like how to make the window, how to make buttons do things, input and output boxes, and stuff like that. I would prefer not to use pyQT or something like that.
The tkinter module is probably the most common Python GUI method.
To make a button:
from tkinter import *
tk = Tk()
btn = Button(tk, text="a clickable button", command())
btn.pack()
To make an input box:
from tkinter import *
tk = Tk()
inputBox = Entry(tk, bd=5)
#to read your box
inputBox.get()
To make a label:
from tkinter import *
tk = Tk()
label = Label(tk, text="your text here")
This question already has answers here:
How can I open a new window when the user clicks the button?
(2 answers)
Closed 6 years ago.
I want to make a gui application in python for that I was making this type of code.
I have already tired many codes but i was not able to make it up to the requirement.
What's stopping you from doing it, please refer the original post here. But basic code:
import Tkinter as tk
def create_window():
window = tk.Toplevel(root)
root = tk.Tk()
b = tk.Button(root, text="Create new window", command=create_window)
b.pack()
root.mainloop()