ValueError: Either a title or a pageid must be specified - python

The code displayed below is giving me a ValueError, explaining I need a title or pagid specified. I have checked the code over and over and do not see a problem. Please let me know if you have any idea what I am doing wrong.
This code is meant to give me information about most key words I enter. If I want Jeff Bezos, information will be printed to the console.
# Imports
import wikipedia
from tkinter import *
import time
# Code
def Application():
# Definitions
def Research():
# Defines Entry
Result = wikipedia.summary(Term)
print(Result)
# Window Specifications
root = Tk()
root.geometry('900x700')
root.title('Wikipedia Research')
# Window Contents
Title = Label(root, text = 'Wikipedia Research Tool', font = ('Arial', 25)).place(y = 10, x = 250)
Directions = Label(root, text = 'Enter a Term Below', font = ('Arial, 15')).place(y = 210, x = 345)
Term = Entry(root, font = ('Arial, 15')).place(y = 250, x = 325)
Run = Button(root, font = ('Arial, 15'), text = 'Go', command = Research).place(y = 300, x = 415)
# Mainloop
root.mainloop()
# Run Application
Application()

You're passing Term to wikipedia.summary(). The error is coming from when summary() creates a page (code). This error happens when there is no valid title or page ID being passed to the page (code). This is happening in your case because you're passing Term straight to summary(), without first converting it to a string. Additionally, Term is a NoneType object, because you're actually setting it to the result of place(). You have to store Term when you create the Entry(), and then apply the place operation to it, in order to be able to keep a reference to it (see here for why):
Term = Entry(root, font = ('Arial, 15'))
Term.place(y = 250, x = 325)
Then, you can get the text value via:
Result = wikipedia.summary(Term.get())

Related

how to generate multiple buttons with a loop?

I have programmed software that displays a "tuile".
Definition of a tuile:
A tuile is a Frame which contains a button which displays an image and an explanatory text.
I would like to display 3 tuiles with 3 different settings.
listes_icones = ["icone1.png","icone2.png","icone3.png"]
listes_relx = [".3",".4",".5"]
listes_text = ["SYSTEM", "USER", "GAME"]
for i in range(3):
gen_img = PhotoImage(file=listes_icones[i])
gen_cadre = Frame(home,width=100, height=100,bg=bg_root)
gen_cadre.place(anchor="c", relx=listes_relx[i], rely=.5)
gen_img_bouton = Button(gen_cadre, image=gen_img, relief="flat",bg=bg_root)
gen_img_bouton.pack()
gen_text = Label(gen_cadre, text=listes_text[i], bg=bg_root, fg=text_color,font="blocktastic 18")
gen_text.pack()
I manage to display the text but not the button and the image, the variable is overwritten. How to solve this problem?
The problem that you are facing is like you said, the variable is overwritten in your loop. To solve this you need to keep track of your generated images. A simple solution is to store them in a list and get them in the next step. Here is an exampel:
import tkinter as tk
import PIL
listes_icones = ["icone1.png","icone2.png","icone3.png"]
gen_icons = []
listes_relx = [".3",".4",".5"]
listes_text = ["SYSTEM", "USER", "GAME"]
home = tk.Tk()
for i in range(3):
gen_img = tk.PhotoImage(file=listes_icones[i])
gen_icons.append(gen_img)
gen_cadre = tk.Frame(home,width=100, height=100)
gen_cadre.place(anchor="c", relx=listes_relx[i], rely=.5)
gen_img_bouton = tk.Button(gen_cadre, image=gen_icons[i], relief="flat")
gen_img_bouton.pack()
gen_text = tk.Label(gen_cadre, text=listes_text[i], font="blocktastic 18")
gen_text.pack()
home.mainloop()

trying to set label text in tkinter

I've been tying to set text to a label but i get this error despite doing the same thing above it with no error, "AttributeError: 'Label' object has no attribute 'set'"
Here is my code and all help is of course appreciated :)
:
#creating the labels
text = StringVar()
text.set("-------")
plate1 = Label(DDFrame1,textvariable = text)
plate1.grid(row=0,column=1)
text1 = StringVar()
text1.set("-------")
owner1 = Label(DDFrame1,textvariable = text1)
owner1.grid(row=1,column=1)
text2 = StringVar()
text2.set("-------")
flags1 = Label(DDFrame1,textvariable = text2)
flags1.grid(row=2,column=1)
def changeText2():
import random
name = ["Vickie Vanfleet","Marcellus Amaker","Cyndi Beale","Roni Foti","Carolyn Sealey",
"Lynda Ansell","Tomiko Kimbrell","Elfreda Bontrager","Melynda Mayberry",
"Precious Nolan","Carl Harm","Trevor Olsen","Anamaria Christianson","Jonna Wagnon",
"Alvina Flock","Sima Lablan","Talisha Fripp","Janey Smedley","Kelly Delpozo",
"Shanice Folse","Sharice Wissing","Darlena Steele","Darlena Steele","Chana Tews",
"Agueda Struble","Harriette Pacifico","Brandon Ellisor","Garry Foushee",
"Telma Kellett","Randa Wojciechowski","Claire Snow","Willa Bankes","Arnold Fall",
"Salome Ridings","Venus Tuner","Willetta Hendriks","Leana Straus"]
name_outcome = random.choice(name)
text1.set(name_outcome)
def changeText3():
import random
reports = ["rep1","rep2","rep3"]
report_outcome = random.choice(reports)
text2.set(report_outcome) #this is the line the error is referencing
Replace text1.set(name_outcome) with text1.configure(text=name_outcome). Same for text2 with report_outcome.
The Label method has no .set() method, you may be confusing it with the StringVar class which does. To work with something like that you would instantiate the label using the textvariable= attribute instead:
text = tkinter.StringVar()
tkinter.Label(root, textvariable=text).pack()
# henceforth usages of text.set("some string") will update the label

.set in function is not being found in other function so it's creating an error tkinter python

I'm trying to create a GUI, in the nav menu you can click a cascade option to open another window where you can click roll to generate a set of numbers. It comes up with error. I think it's because the function is called from another function I just don't know how to get that function to call it/ if there is any other ways to fix this. I've tried global functions and looking it up but haven't found anything other than using classes so far, which I don't know how to do.
line 147, in totalRolls
txtresultsOut.set(totalRollResults)
NameError: name 'txtresultsOut' is not defined
Here is the code that is relevant to it. I've called the function to skip having to input all the other code for the main gui window.
def rollSix():
s = 0
numbers = [0,0,0,0]
for i in range(1,5):
numbers[s] = randrange(1,7)
s += 1
numbers.remove(min(numbers))
Result = sum(numbers)
totalRollResults.append(Result)
def totalRolls():
rollOne()
rollTwo()
rollThree()
rollFour()
rollFive()
rollSix()
txtresultsOut.set(totalRollResults)
def rollw():
rollWindow = tix.Tk()
rollWindow.title("Dice Rolls")
diceLabel = Label(rollWindow, text = "Click Roll for your Stats")
diceLabel.grid(row = 0, column = 0)
rollBtn = Button(rollWindow, text = "Roll Stats", command = totalRolls)
rollBtn.grid(row = 1, column = 0)
txtresultsOut = StringVar()
resultsOut = Entry(rollWindow, state = "readonly", textvariable = txtresultsOut)
resultsOut.grid(row = 2, column = 0)
rollw()
first of all I would NOT recommend using StringVar(). You can use the .get() method of Entry to obtain the value inside the same. Try this way and make a global declaration of the Entry whose values you want to get in other functions.
EDIT------------
#you can use the following code to make your entry active to be edited.
entry.configure(state='normal')
# insert new values after deleting old ones (down below)
entry.delete(0,END)
entry.insert(0, text_should_be_here)
# and finally make its state readonly to not let the user mess with the entry
entry.configure(state='readonly')

Get random text variable from tkinter button - python

The title might be a little confusing, so i will describe my question more.
I making a little program that will assist me with studying Chinese, just for myself. This will aid me with coding and in same time with studying.
I encounter a problem with getting the text variable from my button, without function the code work like wanted. But when trying to get random text that shown on the button it cause me a problem, because text doesn't come. All i need it, when button is pressed function check if input is the same as character and give correct/incorrect notice.
I little new to coding, so it can be simple matter, but still appreciate any help.
The code:
#========== Imports ===========#
from tkinter import *
from tkinter import messagebox
import random
#========== Parameters ==========#
CN = Tk()
CN.title("Chinese Test")
CNW = ["爱","八","爸爸","杯子","北京","本","不客气","不","菜","茶","吃","出租车","打电话",
"大","的","点","电脑","电视","电影","东西","都","读","对不起","多","多少","儿子",
"二","饭店","飞机","分钟","高兴","个","工作","汉语","好","号","喝","和","很","后面","回","会","几","家","叫","今天"]
Cword = ""
Cent = StringVar()
def butPress():
global Cword
if (B0.text==Cword): #wrong way to get text
messageText = "Correct"
else:
messageText = "Incorrect"
CNEntry = Entry(CN,textvariable = Cent).grid(row = 0, column = 1, columnspan = 8)
B0 = Button(CN, text = random.choice(CNW),command = lambda:butPress,bd = 3, width = 5, height = 3).grid(row = 6, column = 4, padx = 10, pady = 10)
#========== Pack ==========#
CN.mainloop( )
There's a few things.
First of all, command = lambda:butPress doesn't work. Use command = butPress. You should only use a lambda when you need to pass parameters (e.g. command = lambda:butPress(parameter)), which you don't.
Then there's B0.text. Because you do
B0 = Button(...).grid(...)
B0 is None, because that is what grid() returns. Change it to
B0 = Button(...)
B0.grid(...)
This way B0 is a Button object. To get the current text of it you can't use B0.text, you have to use B0['text'].
You then compare the text to Cword, which is '' and never changes. If you want to compare it to the entered text in the Entry use CNEntry.get() (after again putting grid on a separate line).

Python Tkinter/GUI Not Updating/Working Correctly

I'm wondering if anyone can give me a quick simple fix for my issue.
I'm trying to make a program (as a gcse mock) that will obtain the position of words in a sentence.
I have the sentence bit working great in the text however I want to go above and beyond to get the highest possible marks so I'm creating it again with a gui!
So far I have the following code and it's not working correctly, it's not updating the 'sentence' variable and I'm looking for a simple way around fixing this Instead of updating. I get some random number which I'm not sure where it has come from. Any help will be much appreciated. :)
#MY CODE:
#GCSE MOCK TASK WITH GUI
import tkinter
from tkinter import *
sentence = ("Default")
window = tkinter.Tk()
window.resizable(width=FALSE, height=FALSE)
window.title("Sentence")
window.geometry("400x300")
#Add custom logo here later on
def findword():
print ("")
sentencetext = tkinter.Label(window, text="Enter Sentence: ")
sentence = tkinter.Entry(window)
sentencebutton = tkinter.Button(text="Submit")
findword = tkinter.Label(window, text="Enter Word To Find: ")
wordtofind = tkinter.Entry(window)
findwordbutton = tkinter.Button(text="Find!", command = findword)
usersentence = sentence.get()
usersentence = tkinter.Label(window,text=sentence)
shape = Canvas (bg="grey", cursor="arrow", width="400", height="8")
shape2 = Canvas (bg="grey", cursor="arrow", width="400", height="8")
#Packing & Ordering Modules
sentencetext.pack()
sentence.pack()
sentencebutton.pack()
shape.pack()
findword.pack()
wordtofind.pack()
findwordbutton.pack()
usersentence.pack()
shape2.pack()
window.mainloop()
Currently your sentence's 'submit' button doesn't actually have a command bound, and the two 'sentence' references are likely to conflict:
sentencebutton = tkinter.Button(text="Submit")
sentence = ("Default")
sentence = tkinter.Entry(window)
I can see that what you've tried to do is set it so that the variable sentence changes from "Default" to whatever one enters in the Entry window - this will not work, all you've done is set it so that sentence becomes the entry widget itself, not whatever is entered.
I would recommend creating a function called something like 'update_sentence', and rename your initial 'sentence' variable to distinguish it from the label:
var_sentence = "default"
def update_sentence:
var_sentence = sentence.get()
And then change your button so it has a command, like so:
sentencebutton = tkinter.Button(text="Submit", command = update_sentence)
Hope this helps!

Categories