非常简单"Random Movie Windows App"从监视列表中获取数据 Trakt.tv



我正在尝试做一个简单的 Windows 应用程序,用于从我的 trakt.tv 监视列表中选择随机电影,我使用 tkinter 作为基本 gui,然后使用随机功能从 txt 文件中选择电影,然后使用 trakt 库功能,我从 trakt 获取我的监视列表并将其记录为 txt 以供随机选择, 为我的一个按钮定义一个函数,以从那里随机选择电影并将其显示在文本框中。它以这种方式工作,但现在我想添加与随机电影相关的概述、评级、年份信息,它仅适用于"某些标题",但有些电影收到以下错误消息;"trakt.errors.NotFoundException: Not Found - 方法存在,但找不到记录">

我认为有些电影的名字不能从 trakt.tv 中得到直接的结果,那么有什么解决方案呢?例如;我可以从监视列表中获取trakt_id并将其用于所有其他信息以及如何使用吗?我检查了库文件,但似乎直接获取用户列表网站的信息,我不知道如何从那里获取任何其他信息。 另外,如何在随机选择的与电影相关的按钮下添加随机同人作品?

对不起我的英语,如果我不能很好地解释我的问题:)

我的代码如下;

# -*- coding: utf8 -*-
import random
from tkinter import *
from trakt import init
from trakt.users import User
from trakt.movies import Movie
import tkinter as tk
### For Trakt.tv Authorize and Save It
### init('myusername', store=True)
my = User('username') ## Must be your username
watchlist = my.watchlist_movies
f=open("watchmovies.txt","w", encoding='utf-8')
f.write(str(watchlist))
f.close()
# TXT DOSYASINI DÜZENLEME
# Read in the file
with open('watchmovies.txt', 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace('[', '')
filedata = filedata.replace(']', '')
# Write the file out again
with open('watchmovies.txt', 'w') as file:
file.write(filedata)
# New Line for Commas
f1=open("watchmovies.txt","r+")
input=f1.read()
print(input)
input=input.replace('<Movie>:','n')
input=input.replace(',','')
f2=open("watchmovies.txt","w+")
f2.write(input)
f1.close()
f2.close()
# Pencere oluşturma
window = Tk()
window.title("Pick A Movie For Me")
window.geometry("300x500")
window.configure(background='black')
# Yazı Alanı oluşturma
T = Text(window, height=15, width=40, wrap = WORD)
T.configure(background='orange')
T.pack()
# Random bilgi için kaynak dosyası
lines = open('watchmovies.txt').read().splitlines()
# Buton işlemi
def callback():
myline = random.choice(lines)
print(myline)
myline2 = Movie(str(myline))
yeary = str(myline2.year)
ratingy = str(myline2.rating)[:4]
overviewy = str(myline2.overview)
T.delete('1.0', END)
showline= "n" + myline + " " + ratingy + "n" + yeary  + "n" + "n" + overviewy
T.insert("1.0", showline)
T.tag_add('center', "1.0", "end")
T.tag_configure("center", justify='center', font='Calibri 11 bold')
# Butonlar
b = Button(window, text="Good Luck!", command=callback)
b.configure(background='black', foreground='white')
b.pack()
c = Button(window, text="Close", command=window.quit)
c.configure(background='black', foreground='white')
c.pack()
mainloop()

我处理完所有事情后最终情况的代码:

# -*- coding: utf8 -*-
import random
import os
from tkinter import *
import tkinter as tk
from trakt import init
from trakt.users import User
from trakt.movies import Movie
from IMDBAPI import IMDB
from tmdbv3api import TMDb
from tmdbv3api import Movie
from urllib.request import urlopen
from PIL import Image, ImageTk
from io import BytesIO
## FOR IMDB, TMDB, TRAKT API 
imdb = IMDB()
tmdb = TMDb()
tmdb.api_key = '<YOUR_TMDB_API_KEY>'
my = User('<YOUR_TRAKT_USER_NAME>')
## Trakt.tv Watchlist 
watchlist = my.watchlist_movies
f=open("watchmovies.txt","w", encoding='utf-8')
f.write(str(watchlist))
f.close()
# TXT FILE
# Read in the file
with open('watchmovies.txt', 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace('[', '')
filedata = filedata.replace(']', '')
# Write the file out again
with open('watchmovies.txt', 'w') as file:
file.write(filedata)
# New Line for Commas
f1=open("watchmovies.txt","r+")
input=f1.read()
print(input)
input=input.replace('<Movie>:','n')
input=input.replace(',','')
f2=open("watchmovies.txt","w+")
f2.write(input)
f1.close()
f2.close()
# File for Random 
lines = open('watchmovies.txt').read().splitlines()
# Button Functions
def callback():
### TEXT INFOS
myline = str(random.choice(lines))
imdbtt = IMDB.getIdFromName(myline)
ratingy = imdb.getRatingByImdbId(imdbtt)
summary = imdb.getSummaryByImdbId(imdbtt)
query_url = 'https://api.themoviedb.org/3/search/movie?api_key=<YOUR_API_KEY>&query='
## ARRANGMENTS FOR TMDB IMAGE SEARCH
myline2 = myline.replace(' ', '%20')
moviename = myline2
title2 = query_url + moviename
link = title2
f = urlopen(link)
myfile = f.read()
f = open("posterurl.txt", "w", encoding='utf-8')
f.write(str(myfile))
f.close()
f3 = open("posterurl.txt", "r+")
input = f3.read()
input = input.replace('\/', 'n')
input = input.replace('jpg"', 'jpgn')
f4 = open("posterurl.txt", "w+")
f4.write(input)
f3.close()
f4.close()
file = open("posterurl.txt", "r")
posterdata = file.readlines()
posterurl = 'https://image.tmdb.org/t/p/w185/' + posterdata[1]
##TEXT SETTINGS
global showline
showline = "n" + myline + "n" + ratingy + "n"
global showline2
showline2 = "n" + summary
## GETTING IMAGE URL
URL = posterurl
u = urlopen(URL)
raw_data = u.read()
u.close()
im = Image.open(BytesIO(raw_data))
global photo
photo = ImageTk.PhotoImage(im)
# Actions to GUI
def guishow():
##IMAGE TO GUI
global window2
window2.pack_forget()
window2 = tk.Label(image=photo)
window2.image = photo
window2.pack()
## TEXTS TO GUI
T.delete('1.0', END)
T.insert("1.0", showline)
T.tag_add('center', "1.0", "end")
T.tag_configure("center", justify='center', font='Calibri 11 bold')
T2.delete('1.0', END)
T2.insert("1.0", showline2)
T2.tag_add('center', "1.0", "end")
T2.tag_configure("center", justify='center', font='Calibri 10 bold')
# GUI: MAIN WINDOW
window = Tk()
window.title("Pick A Movie For Me")
window.geometry("300x600")
window.configure(background='black')
window2 = Frame(window)
window2.pack(side="bottom", expand=True, fill="both")
window2.configure(background='black')
# GUI: TEXT AREA
T = Text(window, height=5, width=40, wrap = WORD)
T.configure(background='darkgray')
T.pack()
# GUI: TEXT AREA 2
T2 = Text(window, height=8, width=40, wrap = WORD)
T2.configure(background='gray')
T2.pack()
## BUTTON FUNCTIONS MERGING
def buttoncase():
callback()
guishow()
# BUTTONS
b = Button(window, text="Good Luck!", command=buttoncase)
b.configure(background='black', foreground='white')
b.pack()
c = Button(window, text="Close", command=window.quit)
c.configure(background='black', foreground='white')
c.pack()
mainloop()

要添加图像,您需要使用图片的文件路径创建一个PhotoImage实例。然后,当您想映射图片时,您有几个可能包含图像的小部件选项,但在这种情况下我建议使用标签。因此,使用您喜欢的关键字创建一个Label实例并设置image=<your image variable>.确保不只是在临时变量上具有 PhotoImage 实例。因此,即当您在类中执行所有这些操作时,您可以将 PhotoImage 变量设置为类变量。否则,您可以将其设置为标签小部件的子变量。最后,这是你自己的选择。对于您的图像,您只能使用.png、.pkg、.gif(据我所知不会移动(以及更多(在 effbot.org 查看(。您也可以使用.bmp但是您需要使用BitmapImage而不是PhotoImage。举个小例子:

from tkinter import Tk,Label,PhotoImage #Python 3.x -> replace 'tkinter' with 'Tkinter' in Python 2.x
root=Tk()
img=PhotoImage(file='yourpath/Image.png')
label=Label(root,image=img)
label.pack()
label.image=img #if not in a class
#self.image=img #if in a class
root.mainloop()

最新更新